2017-08-01 30 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"); 
});