jimmywarting / StreamSaver.js

StreamSaver writes stream to the filesystem directly asynchronous
https://jimmywarting.github.io/StreamSaver.js/example.html
MIT License
3.97k stars 413 forks source link

Write chunk: Buffer to client side #253

Closed Tzvetelin88 closed 2 years ago

Tzvetelin88 commented 2 years ago

Backend is reading fs.createReadStream(...) and sending chunks of a type Buffer to client side. Is it possible to write received chunks one by one to a file on a file system on a client side? I tried to do:

const fileStream = streamSaver.createWriteStream('file_test.zip', {
      size: 2423,
});

and then to pipe chunks to pipeTo.(fileStream)

but didn't worked :(

Tzvetelin88 commented 2 years ago

I was reading the issues and found this one: https://github.com/jimmywarting/StreamSaver.js/issues/215 What I did is to use the approach and it worked.

const fileStream = streamSaver.createWriteStream('file_test.zip', {
      size: this.selectedFile.size
    });
    const writer = fileStream.getWriter();

....
Receiving the chunks from backend as Buffer chunks:
writer.write(new Uint8Array(data.bufferData));
....

and it starts to write the file :)