以下代码正确上传文件,但永远不会命中 onFileUploadData。我已经尝试过使用较旧的 multer 的示例,它似乎可以工作,但不适用于此最新版本。有什么想法吗?
//Server.js
var express = require("express");
var multer = require('multer');
var app = express();
app.use(multer({
dest: './uploads/',
onFileUploadData:function(file, data, res, req){
console.log('onFileUploadData');
}
}).single('userPhoto'));
app.get('/',function(req,res){
res.sendFile(__dirname + "/index.html");
});
app.post('/api/photo',function(req,res){
console.log('ended');
res.end("ended");
});
app.listen(3000,function(){
console.log("Working on port 3000");
});
在 package.json 文件中:
{
"name": "file_upload",
"version": "0.0.1",
"dependencies": {
"express": "4.13.4",
"multer": "1.1.0"
}
}
在视图文件中: 索引.html
<html>
<form id = "uploadForm"
enctype = "multipart/form-data"
action = "/api/photo"
method = "post"
>
<input type="file" name="userPhoto" accept="application/x-zip-compressed,image/*">
<input type="submit" value="Upload Image" name="submit">
</form>
</html>
事件处理程序已从 Multer 中删除。