5
我正在使用jQuery Form Plugin
和multer
將文件上傳到我的服務器。這工作得很好,但我想傳遞一個額外的參數,它將確定文件將保存在哪裏。上傳文件並傳遞附加參數
我有以下的代碼,我謹向表現得說:
HTML
<form id="uploadForm"
enctype="multipart/form-data"
action="/files"
method="post">
<input type="file" id="userFile" name="userFile"/>
<input type="text" hidden id="savePath" name="savePath" value="path"/>
</form>
客戶端JS
uploadForm.submit(function() {
$(this).ajaxSubmit({
error: function(xhr) {
console.log('Error: ' + xhr.status);
},
success: function(response) {
console.log('Success: ' + response);
}
});
return false;
});
Node.js的路線
app.post(APIpath + "file",function(req,res){
var storage = multer.diskStorage({
destination: absoluteServePath+ "/" + config.filePath,
filename: function (req, file, cb) {
cb(null, file.originalname);
}
});
var upload = multer({ storage : storage}).any();
upload(req,res,function(err) {
if(err) {
console.log(err);
return res.end("Error uploading file.");
}
res.end("File has been uploaded");
});
});
注意:我知道我沒有檢查mimetypes或消毒用戶文件,但它現在主要是次要的。
您可以從'req.body'採取'savePath'。 –
好吧,我只是得到'userFile'的對象,'savePath'從來沒有出現在'req.body'中。 –