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

i always gives me Failed to load resource: the server responded with a status of 404 () filename i put in the createWriteStream stream #300

Closed Yassin-Samir closed 1 year ago

Yassin-Samir commented 1 year ago

here is my code

console.log("finished");
if (!window.WritableStream) {
     streamSaver.WritableStream = WritableStream;
      window.WritableStream = WritableStream;
    }
    const fileStream = streamSaver.createWriteStream("packages332.zip");
    const readableStream = new Response(
      await zipper.generateAsync({ type: "blob" })
    ).body;
    if (readableStream.pipeTo) {
      console.log("pipto");
      console.log(fileStream);
      return readableStream.pipeTo(fileStream);
    }
    console.log("not pipto");
    window.writer = fileStream.getWriter();
    const reader = readableStream.getReader();
    const pump = () =>
      reader
        .read()
        .then((res) =>
          res.done ? writer.close() : writer.write(res.value).then(pump));pump();
jimmywarting commented 1 year ago

If you are generating a zip Blob into memory then you might as well just use a link to download the file. ...As the hole point of StreamSaver is to write chunks one chunk after the other directly to the hard drive without accumulating any RAM usage.

const blob = await zipper.generateAsync({ type: "blob" })
const link = document.createElement('a')
const url = URL.createObjectURL(blob)
link.href = url
link.download = 'package332.zip'
link.click()
URL.revokeObjectURL(url)
Yassin-Samir commented 1 year ago

i can't cause this zip file file gets its files from s3 bucket aws so i wrap all these files into the zip

jimmywarting commented 1 year ago

yes. And when you do wrap all your s3 files into a zip file then you do get a Blob back from running await zipper.generateAsync({ type: "blob" }) ...then you already got a complete final Blob that you can use to save the file using traditional a[download][href=bloburll] tech.

if you need to save smaller blobs then it's easier to just save them using traditional download links or using FileSaver.js

jimmywarting commented 1 year ago

i assume zipper.generateAsync comes from jszip?

Yassin-Samir commented 1 year ago

yeah thanks