file上传节点js

问题描述 投票:-1回答:2
app.post('/upload/notes',function(req,res){
     if(req.files){
         console.log("files");
        // filename is the id of the html input field
         var file = req.files.filename;
             }
}

现在我正在尝试使用改造的Android设备上传文件。改造中的id等价物是什么。如果不是这样,请建议一种方法,我可以使用邮递员上传图像或文件,我将使用上面给出的路线处理。 enter image description here

如图所示,有一个“上传/备注”的路线,但由于某种原因它不会加载。我是节点的新手,我无法理解这个问题。

node.js express
2个回答
-1
投票

https://stackoverflow.com/a/43041225/6640982

在Express 4中,默认情况下req.files在req对象上不再可用。要访问req.files对象上的上传文件,请使用multipart-handling中间件,如busboy,multer,formidable,multiparty,connect-multiparty。


-2
投票
    //**create a page name as fileUplaodTesting.js which consist of html form too**
   var http = require('http');
var fs = require('fs');
var formidable = require('formidable');
http.createServer(function (req, res) {

    if (req.url == '/fileupload') {

        var form = new formidable.IncomingForm();
        form.parse(req, function (err, fields, files) {
      var oldpath = files.filetoupload.path;
      var newpath = './public/' + files.filetoupload.name;
      fs.rename(oldpath, newpath, function (err) {
        if (err) throw err;
        res.write('File moved Succesfully');
         res.end();
      });
 });

  }else
  {
     res.writeHead(200, {'Content-Type': 'text/html'});
      res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
      res.write('<input type="file" name="filetoupload"><br>');
      res.write('<input type="submit">');
      res.write('</form>');
      return res.end();
  }

}).listen(8080);
© www.soinside.com 2019 - 2024. All rights reserved.