jimmywarting / StreamSaver.js

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

Incomplete download when write to the streamSaver #339

Open Zhangjiawei-Beinloft opened 6 months ago

Zhangjiawei-Beinloft commented 6 months ago

I follow the example of "Piping a fetch response to StreamSaver". When I download small file like 1KB, the code below works and it can console log the message "done writing". But I download bigger file like 57.7KB, readableStream.pipeTo(fileStream) never finish and the http request is not finished all the time.

I'm not sure if the write operation is blocking the reception of the data stream?Or is it something else?

The code:

fetch(url).then(res => {
          const readableStream = res.body
          if (window.WritableStream && readableStream.pipeTo) {
            return readableStream.pipeTo(fileStream)
              .then(() => console.log('done writing'))
          }
          window.writer = fileStream.getWriter()
          const reader = res.body.getReader()
          const pump = () => reader.read()
            .then(res => res.done
              ? writer.close()
              : writer.write(res.value).then(pump))
          pump()
        })
Zhangjiawei-Beinloft commented 6 months ago

微信图片_20240313094630