Error "Unexpected end of multipart data" in busboy file upload
Asked Answered
E

5

12

I am using connect-busboy to upload file in node/express app.The problem is sometimes it works(file get uploaded succsesfully) and sometimes i get error Unexpected end of multipart data and the application crash.What could be the cause of this error? Also any help on how to debug this will be appreciated. I am using node version 5 and connect-busboy": "0.2.14"Thank you in advance

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);

});

This is the error i am getting

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
Excuse answered 11/5, 2017 at 2:46 Comment(3)
Unrelated tip: never use the filename as-is. It is client-supplied and could be any value (including a malicious value that could be a relative path outside of your intended destination directory). Instead, use a random filename or even a hash of filename would be ok.Disconsolate
Thanks for the tip @DisconsolateExcuse
Am getting the exact same error using a C# client: "Error: Unexpected end of multipart data at c:\Code\NodeJSHW\node_modules\dicer\lib\Dicer.js:62:28 at _combinedTickCallback (internal/process/next_tick.js:95:7) at process._tickCallback (internal/process/next_tick.js:161:9)"Trivandrum
N
2

For me, I received this error when I used \n newlines instead of \r\n newlines when formatting my post body on the client side.

When I fixed the newlines (as seen in code below) it worked.

fetch('/api/upload', 
  { method: 'POST',
    credentials: 'include',
    headers: {'Content-type': 'multipart/form-data; boundary=XXX' },
    body: '--XXX\r\nContent-Disposition: form-data; name="file"; filename="filename.csv"\r\nContent-Type: text/csv\r\n\r\nA,B,C\r\n1,1.1,name1\r\n2,2.2,name2\r\n\r\n--XXX--'
  });
Northernmost answered 16/7, 2018 at 23:34 Comment(0)
C
2

You could be missing the closing dashes ('--') of the final boundary.

https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

example:

Content-Type: multipart/alternative; boundary=boundary42 


--boundary42 
Content-Type: text/plain; charset=us-ascii 

...plain text version of message goes here.... 

--boundary42 
Content-Type: text/richtext 

.... richtext version of same message goes here ... 
--boundary42 
Content-Type: text/x-whatever 

.... fanciest formatted version of same  message  goes  here 
... 
--boundary42-- 
Cornwall answered 3/6, 2020 at 14:44 Comment(1)
My problem was that each boundary must also begin with an extra "--". When I wrote my Content-Type header, I copied the boundary= directly from the body without removing those extra hyphens.Jubbah
B
1

If anybody else is having an issue with this still, my issue was something on the front end. Using Swift, it seems like there was an issue if you use the default URLSession config. I changed it from:

let session = URLSession(configuration: .default, delegate: self, delegateQueue: nil)

to the following:

  let sessionConfig = URLSessionConfiguration.background(withIdentifier: "it.yourapp.upload")
        sessionConfig.isDiscretionary = false
        sessionConfig.networkServiceType = .default

        let session = URLSession(configuration: sessionConfig, delegate: self, delegateQueue: OperationQueue.main)

and now it works great!

Backed answered 3/12, 2019 at 4:45 Comment(0)
S
1

Okay so I was also facing this issue, I tried adding this header in app side 'Content-Type': 'multipart/form-data; boundary=XXX', And it worked out :)

Smoothen answered 18/10, 2021 at 7:17 Comment(0)
A
0

This is a bug related to firebase tools. I came across this issue with busboy package today and it cost me 2hrs to fix this issue. We just need to upgrade the firebase tools to fix this issue.

Case 1: If you've installed firebase tools as your package dependency, run below code

npm i firebase-tools

Case 2: If you've installed firebase tools as global dependency,run below code

npm i -g firebase-tools

Working Version of firebase tools:

enter image description here

I Hope this helps, for more info checkout this issue link.

Arrington answered 14/7, 2019 at 10:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.