Reputation: 511
I am streaming jpg file to a node server.But the issue is sometimes it works and some times it fails with the folowing error and crashes my server
events.js:183
066466+00:00 app[web.1]: throw er; // Unhandled 'error' event
2018-06-19T00:45:07.066468+00:00 app[web.1]: ^
2018-06-19T00:45:07.066470+00:00 app[web.1]:
066472+00:00 app[web.1]: Error: Unexpected end of multipart data
2018-06-19T00:45:07.066474+00:00 app[web.1]: at
/app/node_modules/busboy/node_modules/dicer/lib/Dicer.js:62:28
Here is my code to handle the file
req.busboy.on('file', function (fieldname, file, filename) {
console.log("Uploading: " + (filename));
fstream = fs.createWriteStream(__dirname + '/public/img/'+ filename);
file.on('data', function(chunk) {
fstream.write(chunk);
});
file.on('end', function() {
fstream.end();
console.log('File [' + fieldname + '] Finished sucessfully');
});
file.on('error',function(err){
console.log('fstream error' + err);
file.unpipe();
});
});
Can somebody please help what could be the issue here? Thank you in advance.
Upvotes: 3
Views: 2993
Reputation: 511
Ok answering my own question.I was not handling error event correctly.Here is the working code.
var fstream;
req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) {
console.log("Uploading: " + (filename));
fstream = fs.createWriteStream(__dirname + '/public/img/'+ filename);
file.on('data', function(chunk) {
fstream.write(chunk);
});
file.on('error',function(err){
console.log('fstream error ' + err);
});
file.on('end', function() {
fstream.end();
console.log('File [' + fieldname + '] Finished sucessfully');
});
});
req.busboy.on('error',function(err){
console.log('req.busboy error' + err);
});
Upvotes: 3