SQL Server Convert binary data (varbinary) to ReadbleStream in NodeJs

r3i60tvu  于 2023-06-28  发布在  其他
关注(0)|答案(1)|浏览(109)

I'm receiving some file buffer (binary data) and I'm storing them in our SQL Server.

My service is a NodeJs Backend service which the frontend mobile app with interract with.

They will upload a file (img/pdf) that I need to store into our db (sql server) (I don't know if it is a good idear...)

Then I need to send this file to a thirdparty service to control. I'm using multer to retrieve file data from frontend, and using MemoryStorage of multer as storage option, i can only get buffer data of this file. So I have to "restore" the buffer to a image or pdf file to send to the thirdparty service via API call with multpart/form-data.

I have found a library nodejs called Readable and I'm using directly:

let data = FormData();
let readable = Readable.from(myBuffer);
data.append('files[]', readable);

I don't know if I only need to pass the readable._readableState to DataForm which seems like the same compare to what i received from the frontend when they uploaded the file.

But if I do so, i got an error: source.on is not a function

So I want to know if there is a proper way to "restore" or create a file from a binary buffer <Buffer ff d8 fs ... 24200 more bytes> in nodejs? In order to obtains the same behaviour than the fs.createdReadbleSteam() which need a file path.

Thanks in advance.

o3imoua4

o3imoua41#

Finally, I had not to convert the Buffer to ReadableStream . It's much simplier than what i imagined.

For people who are facing the same problem, you have just to pass your buffer into the FormData then specify the filename and contenType of file to "restore" it:

data.append('files[]', 'yourBuffer', {
  filename: yourFileName,
  contentType: yourFileMimeType
});

相关问题