0

I am trying to recieve an image via busboy in Node.js post call however I am getting the following error:

Error: Unexpected end of multipart data
thrown by 
..\node_modules\dicer\lib\Dicer.js:61:28

This is my code:

busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
        console.log('ok', fieldname, file, filename, encoding, mimetype);
        if (mimetype !== 'image/jpeg' && mimetype !== 'image/png') {
          return res.status(400).json({ error: 'Wrong file type submitted' });
        }
        // my.image.png => ['my', 'image', 'png']
        const imageExtension = filename.split('.')[filename.split('.').length - 1];
        // 32756238461724837.png
        imageFileName = `${Math.round(
          Math.random() * 1000000000000
        ).toString()}.${imageExtension}`;
        const filepath = path.join(os.tmpdir(), imageFileName);
        imageToBeUploaded = { filepath, mimetype };
        file.pipe(fs.createWriteStream(filepath));
    });

I hope you can enlighten me on why this error is occuring as I am new to node.js

André Kool
  • 4,880
  • 12
  • 34
  • 44
Adhil
  • 1,678
  • 3
  • 20
  • 31

1 Answers1

2

It would be useful to have some more details about your server and client. For instance:

  • Are you trying to receive it in a Firebase/Google cloud function? Then you may need to pass to Busboy the rawReq field, and not the request itself.
  • On your client, did you specify the header of the request as multipart/form-data and included the boundary? Something like:
const config = { headers: { 'content-type': `multipart/form-data; boundary=${form_data._boundary}` }};

You may want to try call your api from some debugging tool like Postman and try to understand if the problem is on the server or in some missing parameter from the client.

Bruno
  • 148
  • 8