如何使用快速会话上传文件?

时间:2017-12-01 02:24:27

标签: node.js express session file-upload 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有数据!

但是当我向我的app.js添加express-session时更改req和req.files不再存在

1 个答案:

答案 0 :(得分:1)

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

app.use(fileUpload());