访问从客户端发送到服务器的FormData对象(Express)

时间:2017-02-11 20:59:21

标签: angular express

我正在实施此解决方案,用于将文件上传到服务器:File Upload with Angular2 to Rest API

我的问题是如何在服务器端POST方法中访问formData项目?

代码:

    router.post('/test', function(req, res) {
        // Access the formData object sent from the client
        // Do stuff...
    }

谢谢!

1 个答案:

答案 0 :(得分:0)

使用以下代码: -

 router.post('/test', function(req, res) {
     let formidable = require('formidable'); 
     let form = new formidable.IncomingForm(); 
     form.parse(req, function(err, fields, files) {
          // fields will get all fields
          if(files.file && files.file.name){ 
               //file your uploaded file
          }
     });
  });