jimmywarting / StreamSaver.js

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

can I download multiple files at the same time? #130

Closed jinshengli closed 4 years ago

jinshengli commented 4 years ago

I had a problem downloading multiple files in a loop. The firefox Browser error: TypeError: The stream (in closed state) is not in the writable state and cannot be closed.

I can only download the first file. After that faile. I canot find a good way

my code:

url: [ {fileName: url } ]

getDownFile: function(url, fileName) {
    let u = url[fileName];
    let fileStream = streamSaver.createWriteStream(fileName);
    fetch(u).then(res => {
      const readableStream = res.body;
      // more optimized
      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();

    })
}

for (var fileName in url) {
  vm.getDownFile(url, fileName);
}
jimmywarting commented 4 years ago

Try changing this:

-      window.writer = fileStream.getWriter();
+      const writer = fileStream.getWriter();

ps, if you like you could try the saving-multiple-files example and save everything as one zip file

jinshengli commented 4 years ago

Thank you. It work.

amiaynara commented 2 years ago
et fileStream = streamSaver.createWriteStream(fileName);

@jinshengli the change suggested by @jimmywarting work( changing window.writer to writer)? Or did you use the saving-multiple-files option instead?

Lwdthe1 commented 7 months ago

This worked for me with the suggested typo fix