【发布时间】:2017-10-09 21:09:10
【问题描述】:
我正在使用 connect-busboy 在 node/express 应用程序中上传文件。问题是有时它可以工作(文件成功上传),有时我收到错误 Unexpected end of multipart data 和应用程序崩溃。这可能是什么原因错误?也将不胜感激有关如何调试此问题的任何帮助。我正在使用node version 5 和connect-busboy": "0.2.14"提前谢谢你
router.route('/images')
.post (function(req, res) {
var fstream;
req.busboy.on('file', function (fieldname, file, filename) {
fstream = fs.createWriteStream(__dirname + '/public/img/'+ filename);
file.pipe(fstream);
file.on('end', function() {
console.log('File [' + fieldname + '] Finished sucessfully');
});
fstream.on('error',function(err){
console.log('fstream error' + err);
file.unpipe();
});
fstream.on('close', function () {
res.status(200);
res.json({ message: 'File uploaded' });
});
});
req.pipe(req.busboy);
});
这是我遇到的错误
throw er; // Unhandled 'error' event
: Error: Unexpected end of multipart data
2017-05-07T20:28:27.599826+00:00 app[web.1]: at
/app/node_modules/busboy/node_modules/dicer/lib/Dicer.js:62:28
【问题讨论】:
-
无关提示:永远不要按原样使用
filename。它是客户端提供的,可以是任何值(包括可能是预期目标目录之外的相对路径的恶意值)。相反,使用随机文件名甚至是filename的哈希就可以了。 -
感谢@mscdex 的提示
-
使用 C# 客户端时出现完全相同的错误:“错误:c:\Code\NodeJSHW\node_modules\dicer\lib\Dicer.js:62:28 at _combinedTickCallback 的多部分数据意外结束(internal/process/next_tick.js:95:7) 在 process._tickCallback (internal/process/next_tick.js:161:9)"
标签: node.js express multer busboy