如何使用 express-session 上传文件?

How to upload a file using express-session?

我有一个 express 应用程序,我需要上传一个文件,我目前正在使用 express-fileupload,在测试中我没有遇到任何问题:

const fs = require('fs');
const fileUpload = require('express-fileupload');
const resizeImg = require('resize-img');

app.post('/upload', function(req, res) {
  if (!req.files)
    return res.status(400).send('No files were uploaded.');
  let sampleFile = req.files.sampleFile;
  var d = new Date();
  var newName = d.getHours()+'-'+d.getMinutes()+'-'+d.getSeconds()+'.jpg';
  console.log(newName)
  var rImage = 'C:/Developer/node/express-fileuploadWebix/public/images/'+newName;
  sampleFile.mv(rImage, function(err) {
    if (err)
    {
      return res.status(500).send(err);
    }
    resizeImg(fs.readFileSync(rImage), {width: 130, height: 180}).then(buf => {
      fs.writeFileSync(rImage, buf);
    });
    res.send('File uploaded!');
  });
});

req.files.sampleFile有数据!

但是当我将 express-session 添加到我的 app.js 时。更改请求,req.files 不再存在

实际上 运行 fileUpload 中间件,您需要在请求处理程序之前添加这行代码:

app.use(fileUpload());