Home > Net >  Is there a way to put Express.Multer.File into a FormData object?
Is there a way to put Express.Multer.File into a FormData object?

Time:11-12

I'm need help on finding how to put a file with a type Express.Multer.File into a FormData object.

Context: I'm receiving a file with a type Express.Multer.File (I'm using nestjs, so I used this part of the documentation : https://docs.nestjs.com/techniques/file-upload) that I need to put in a FormData, to send it to another API.

I tried form.append('randomword', file); but I get the following error :

TypeError: source.on is not a function
    at Function.DelayedStream.create (xxx/delayed-stream/lib/delayed_stream.js:33:10)
    at FormData.CombinedStream.append (xxx/combined-stream/lib/combined_stream.js:45:37)
...

I clearly sense that I'm doing it the wrong way, but I'm kinda lost on how to achieve something like this.

Could someone help me please ?

CodePudding user response:

I found out a "workaround" for this method.

  • First, I get the file with the type Express.Multer.File.
  • Then I save it using createWriteStream from fs.
  • Finally, I pass it into the FormData object using createReadStream.

And done ! It isn't the best way to do this, but kind of a workaround, though.

  • Related