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'中。 –