2017-08-25 78 views
0

我有一個MEAN堆棧應用程序,並嘗試使用multer上傳文件。上傳工作正常,唯一的問題是我想返回上傳的文件的ID。如何返回上傳文件的id(在nodejs express應用程序中使用multer上傳)

//multer settings for single upload 
var upload = multer({ storage: storage }).single('file'); 

app.post('/upload', upload, function (req, res) { 
//// ????? What to return and how? 
}); 

我訂閱了這篇文章到angular2服務。 謝謝你的幫助。

+0

@rckrd它的工作原理這種方式,我把它作爲第二參數在後。 – 1011sophie

+0

但無論如何,如果我把它稱爲後像這裏面: app.post( '/上傳',函數(REQ,RES){ 上傳(REQ,RES功能(ERR){ 如果(ERR){ res.json({error_code:1,err_desc:err}); return; } console.log(res); }); }); 在控制檯我看到大對象,其中也包含文件對象,它有'id',這是我想發送回 – 1011sophie

回答

1
var multer = require('multer'); 

// storage for upload file. 
var storage = multer.diskStorage({ 
    destination: function (req, file, callback) { 
    callback(null, './file'); 
    }, 
    filename: function (req, file, callback) { 
    callback(null, file.originalname); 
    } 
}); 

var Upload = multer({ 
    storage: storage 
}).any('file'); 


router.post('/upload', postData); 

function postData(req, res) { 
Upload(req, res, function (error) { 
    if (error) { 
     res.status(400).send(error); 
    } 
    var obj = {}; 
    obj.file = req.files.filename; 
    //file is getting stored into database and after it successfully stored 
    //into database it will return you Id 
    db.create(obj, function (err, data) { 
         if (err) { 
          res.send('error'); 
         } 
         if (!data) { 
          res.send('Error'); 
         } else { 
          console.log('file upload'); 
          res.send(data._id); 
         } 
        }); 
      }); 
     } 

返回標識,你應該有存儲在數據庫中的某個地方之後,你將返回ID來Angular2

0

請求文件對象(req.file)不包含任何唯一ID號,你將不得不推出自己的ID分配邏輯。

您將不得不考慮您需要該ID的目的,然後您可以使用存儲在請求文件對象(req.file)中的信息使其對該文件具有特定的含義。

例如,如果所有文件都存儲在相同的路徑中,並且稍後將使用ID來檢索該文件,那麼您必須考慮採用不會給用戶輸入帶來問題的策略。


multer上傳的每個文件都包含以下信息:

**:Key:**  **:Description:** 

fieldname  Field name specified in the form  
originalname Name of the file on the user's computer  
encoding  Encoding type of the file 
mimetype  Mime type of the file 
size   Size of the file in bytes 
destination  The folder to which the file has been saved (DiskStorage only) 
filename  The name of the file within the destination (DiskStorage only) 
path   The full path to the uploaded file   (DiskStorage only) 
buffer   A Buffer of the entire file     (MemoryStorage only) 

所以,你可以使用文件名(表單中使用)req.file.fieldname

還是原來的文件req.file.originalname(但使用originalname如果你上傳的文件名相同multipletime只能產生一些問題,所以......)

更妙的是,通過將當前日期時間戳記創建文件的唯一字符串與filename/original,如:(使用字符串timestamp-filename.ext例子)timestamp-filename.ext

或生成一個隨機哈希 5910c2f5df0471320b8fc179fa6dc1114c1425752b04127644617a26a373694a(SHA256)

+0

在res函數中存在id,我也看到這個id在DB表中。問題是我不能退還它。 – 1011sophie

0

如果您正在使用蒙戈DB(GridFS的流)的上傳功能的資源對象具有對象的文件存儲res.req.file下它具有所有如下上傳文件的元數據和網格細節細節:

{ 
    fieldname:'file', 
    originalname:'Screenshot (1).png', 
    encoding:'7bit', 
    mimetype:'image/png', 
    filename:'file-1516342158216.png', 
    metadata:{ 
     originalname:'Screenshot (1).png', 
     email:'[email protected]', 
     imageType:'ProfilePic' 
    }, 
    id:5 a618b8e1d9af11f388dcabb, 
    grid:{ 
     _id:5  a618b8e1d9af11f388dcabb, 
     filename:'file-1516342158216.png', 
     contentType:'image/png', 
     length:675410, 
     chunkSize:261120, 
     uploadDate:2018-01  -19  T06:09:18.736  Z, 
     aliases:undefined, 
     metadata:{ 
     originalname:'Screenshot (1).png', 
     email:'[email protected]', 
     imageType:'ProfilePic' 
     }, 
     md5:'fdda12a2f098a915c24162dc410cb3b6' 
    }, 
    size:undefined 
}