2017-08-01 25 views
0

我需要设置文件上传路径基于表单字段值在一个强大的表单上载,同时具有文件和字段(多部分表单类型)。节点强大的重命名目录路径从表单字段

theForm.on( 'fileBegin',函数(名称,文件){...} 之前 theForm.parse(REQ,功能(ERR,字段,文件){...}称为

然而,似乎表单上传路径必须在解析表单字段之前设置好,所以我没有看到访问字段属性的方法,除了原型外,没有任何东西存在,我也查看了req。但这些值不存在

这是正确的吗?有没有办法在表单字段可用后更改表单上传路径,但在文件保存到磁盘之前?

我使用的是当今最新的一切,2017年7月31日。

我也有身体分析器在使用,阅读在JSON ..可能是造成这个问题? (我已经读过,它也是不好的,使用它 - 删除它导致我的其他问题,所以我已经离开它到目前为止..) const bodyParser = require('body-parser'); const jsonParser = bodyParser.json();

非常感谢!

回答

0

我会建议使用express-fileupload模块:

var express = require('express'); 
var app = express(); 
var path = require('path'); 

const fileUpload = require('express-fileupload'); 
app.use(fileUpload()); 

var defaultDir = "C:\\temp"; 
app.post('/upload', function(req, res){ 

    // The name of the input field (i.e. "clientfile") is used to retrieve the uploaded file 
    let sampleFile = req.files.clientfile; 

    var savefile_path = path.join(defaultDir, req.body.path, sampleFile.name); 

    // Use the mv() method to place the file somewhere on your server 
    sampleFile.mv(savefile_path, function(err) { 
     if (err) 
     { 
      return res.status(500).send(err); 
     } 
    }); 
    res.status(200).send("File uploaded"); 
    console.log("File uploaded"); 
});