2017-08-25 83 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 
}