如何使用ExpressJS 4上传文件?(复制)

时间:2021-10-22 23:15:31

This question already has an answer here:

这个问题已经有了答案:

From ExpressJS 4 API, I find the req.files was invalid. How to upload files use ExpressJS 4 now?

从ExpressJS 4 API,我找到了req。文件是无效的。如何上传文件现在使用ExpressJS 4 ?

2 个解决方案

#1


3  

express.bodyParser is not included in express 4 by default. You must install separately. See https://github.com/expressjs/body-parser

表达。bodyParser默认不包含在express 4中。你必须单独安装。参见https://github.com/expressjs/body-parser

Example :

例子:

var bodyParser = require('body-parser');

var app = connect();

app.use(bodyParser());

app.use(function (req, res, next) {
  console.log(req.body) // populated!
  next();
})

There is also node-formidable

还有node-formidable

var form = new formidable.IncomingForm();

form.parse(req, function(err, fields, files) {
  res.writeHead(200, {'content-type': 'text/plain'});
  res.write('received upload:\n\n');
  res.end(util.inspect({fields: fields, files: files}));
});

return;

Here is how I did it:

我是这样做的:

form = new formidable.IncomingForm();
form.uploadDir = __dirname.getParent() + "/temp/";
form.parse(req, function(err, fields, files) {
  var newfile, path, uid, versionName;
  uid = uuid.v4();
  newfile = __dirname.getParent() + "/uploads/" + uid;
  copyFile(files.file.path, newfile, function(err) {
    if (err) {
      console.log(err);
      req.flash("error", "Oops, something went wrong! (reason: copy)");
      return res.redirect(req.url);
    }
    fs.unlink(files.file.path, function(err) {
      if (err) {
        req.flash("error", "Oops, something went wrong! (reason: deletion)");
        return res.redirect(req.url);
      }
      // done!
      // ...
    });
  });
});

#2


22  

I just had this issue after upgrading, where req.files was undefined. I fixed it by using multer.

我刚升级完这个问题,req。文件是未定义的。我用了莫特。

So,

所以,

npm install multer

and then in your app.js

然后在app.js中。

var multer = require('multer');
app.use(multer({ dest: './tmp/'}));

I didn't have to change anything else after that and all my old functionality worked.

在那之后,我不需要改变任何东西,我所有的旧功能都起作用了。

#1


3  

express.bodyParser is not included in express 4 by default. You must install separately. See https://github.com/expressjs/body-parser

表达。bodyParser默认不包含在express 4中。你必须单独安装。参见https://github.com/expressjs/body-parser

Example :

例子:

var bodyParser = require('body-parser');

var app = connect();

app.use(bodyParser());

app.use(function (req, res, next) {
  console.log(req.body) // populated!
  next();
})

There is also node-formidable

还有node-formidable

var form = new formidable.IncomingForm();

form.parse(req, function(err, fields, files) {
  res.writeHead(200, {'content-type': 'text/plain'});
  res.write('received upload:\n\n');
  res.end(util.inspect({fields: fields, files: files}));
});

return;

Here is how I did it:

我是这样做的:

form = new formidable.IncomingForm();
form.uploadDir = __dirname.getParent() + "/temp/";
form.parse(req, function(err, fields, files) {
  var newfile, path, uid, versionName;
  uid = uuid.v4();
  newfile = __dirname.getParent() + "/uploads/" + uid;
  copyFile(files.file.path, newfile, function(err) {
    if (err) {
      console.log(err);
      req.flash("error", "Oops, something went wrong! (reason: copy)");
      return res.redirect(req.url);
    }
    fs.unlink(files.file.path, function(err) {
      if (err) {
        req.flash("error", "Oops, something went wrong! (reason: deletion)");
        return res.redirect(req.url);
      }
      // done!
      // ...
    });
  });
});

#2


22  

I just had this issue after upgrading, where req.files was undefined. I fixed it by using multer.

我刚升级完这个问题,req。文件是未定义的。我用了莫特。

So,

所以,

npm install multer

and then in your app.js

然后在app.js中。

var multer = require('multer');
app.use(multer({ dest: './tmp/'}));

I didn't have to change anything else after that and all my old functionality worked.

在那之后,我不需要改变任何东西,我所有的旧功能都起作用了。