serviejs/popsicle

How to set the filename using multipart form data and raw stream of buffer?

Closed this issue · 5 comments

Sending form data works well when using streams created with fs but I'm actually trying to upload data from a Node buffer, and unfortunately that doesn't work well.

It seems that I'm missing the part where the filename is specified in the form data, and I found nothing in your source code to help.
When transmitting form data, it should look like this:

'Content-Disposition: form-data; name="file"; filename="myfile.ext" [...]

What I'm actually missing is how to set the filename="myfile.ext" part without using a stream from fs?

In any case someone needs it, here is the solution:

popsicle.form only support uploading files using standard file stream, to work with buffer or other stream type you need to use form-data directly:

    const form = new FormData();
    form.append('file', mystream, {
      filename: myfilename,
      knownLength: myfile.length
    });

It would be nice if popsicle allowed to set those options though.

@sinedied popsicle.form returns an instance of form-data, so it allows all the same features to work as you've just used. Just call const form = popsicle.form() and append after it.

I'd love to improve the README with this note! 😄

popsicle.form is undefined in popsicle 12.0.1

@Obapelumi You can use form-data directly nowadays. This was largely before form-data has a browser aliased version (e.g. https://github.com/form-data/form-data/blob/d7026253e728af9568503dc3dc55cd1a566605e0/package.json#L11).