Node.js 如何返回上传文件的id(在nodejs express应用程序中使用multer上传)

Node.js 如何返回上传文件的id(在nodejs express应用程序中使用multer上传),node.js,express,multer,Node.js,Express,Multer,我有一个MEAN stack应用程序,正试图用multer上传文件。上传工作正常唯一的问题是我想返回上传文件的id //multer settings for single upload var upload = multer({ storage: storage }).single('file'); app.post('/upload', upload, function (req, res) { //// ????? What to return and how? }); 我已经订阅了

我有一个MEAN stack应用程序,正试图用multer上传文件。上传工作正常唯一的问题是我想返回上传文件的id

//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服务。 谢谢你的帮助

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,您必须将引用存储在数据库的某个位置,然后将Id返回给Angular2

要返回Id,您必须将引用存储在数据库的某个位置,然后将Id返回给Angular2

请求文件对象(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
只能在多次上载相同的文件名时产生问题,所以…)

更好的是,通过将当前日期时间戳与
filename/original
相结合,例如:
timestamp filename.ext

或者生成一个随机散列(例如使用字符串
timestamp filename.ext
5910c2f5df0471320b8fc179fa6dc1114c1425752b04127644617a26a373694a
(SHA256)

请求文件对象(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
只能在多次上载相同的文件名时产生问题,所以…)

更好的是,通过将当前日期时间戳与
filename/original
相结合,例如:
timestamp filename.ext

或者生成一个随机散列(例如使用字符串
timestamp filename.ext
5910c2f5df0471320b8fc179fa6dc1114c1425752b04127644617a26a373694a
(SHA256)

如果您使用Mongo DB(GridFs流)存储文件,则上载函数的res对象在
res.req.file
下具有上载文件的元数据和网格详细信息的所有详细信息,如下所示:

{  
   fieldname:'file',
   originalname:'Screenshot (1).png',
   encoding:'7bit',
   mimetype:'image/png',
   filename:'file-1516342158216.png',
   metadata:{  
      originalname:'Screenshot (1).png',
      email:'prasan.g8@gmail.com',
      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:'prasan.g8@gmail.com',
         imageType:'ProfilePic'
      },
      md5:'fdda12a2f098a915c24162dc410cb3b6'
   },
   size:undefined
}
如果您使用Mongo DB(GridFs stream)存储文件,则上载函数的res对象将对象置于
res.req.file
下,其中包含上载文件的元数据和网格详细信息的所有详细信息,如下所示:

{  
   fieldname:'file',
   originalname:'Screenshot (1).png',
   encoding:'7bit',
   mimetype:'image/png',
   filename:'file-1516342158216.png',
   metadata:{  
      originalname:'Screenshot (1).png',
      email:'prasan.g8@gmail.com',
      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:'prasan.g8@gmail.com',
         imageType:'ProfilePic'
      },
      md5:'fdda12a2f098a915c24162dc410cb3b6'
   },
   size:undefined
}

在res of upload函数中存在id,我在DB表中也看到了这个id。。问题是我不能返回它。在上传函数的res中存在id,我在DB表中也看到了这个id。。问题是我不能返回它。@rckrd它也是这样工作的,我在post中作为第二个参数传递它。但是无论如何,如果我在post中这样调用它:app.post('/upload',function(req,res){upload(req,res,function(err){if(err){res json({error\u code:1,err desc:err});return;}console.log(res);});});在控制台中,我看到一个大对象,它也包含文件对象,它有
id
,这就是我想要发送的内容back@rckrd它也是这样工作的,我在post中作为第二个参数传递它。但是无论如何,如果我在post中这样调用它:app.post('/upload',function(req,res){upload(req,res,function(err){if(err){res.json({error_code:1,err_desc:err});return;}console.log(res);});});在控制台中,我看到一个大对象,它也包含文件对象,它有
id
,这就是我想要返回的