StreamSaver.js icon indicating copy to clipboard operation
StreamSaver.js copied to clipboard

How to let StreamSaver "fail" the download?

Open Menci opened this issue 4 years ago • 6 comments

I'm download files with fetch and saving them to a zip file. If a network error occurs while fetching, how can I tell browser the download failed rather than just aborting it? aborting makes browser turn the download item to finished.

Menci avatar Jan 21 '20 10:01 Menci

Aborting should cancel the download and delete the data that have been written. closing it will end the download normally?

if you pipe stuff that fails then you could try stuff like preventAbort: true

pipeTo(dest, { preventClose, preventAbort, preventCancel, signal } = {})

Think i'm going to need a code example to understand what you are doing.

jimmywarting avatar Jan 21 '20 11:01 jimmywarting

https://github.com/jimmywarting/StreamSaver.js/blob/master/examples/saving-multiple-files.html#L77

If here a network error occurs in the fetch, what should I do to tell browser to show the download as "error"?

image

Menci avatar Jan 29 '20 08:01 Menci

maybe this will help:

readableZipStream.pipeTo(fileStream)
  .catch(err => {
    console.log('failed to save the zip...')
  })

or while getting the stream

try {
  const res = await fetch(url)
  if (res.ok) { // status 2xx
    res.body
  } else {
    // the response returned none 2xx response
    // continue with the next url or cancel the hole stream
  }
} catch (err) {
  // network error
}

jimmywarting avatar Jan 29 '20 09:01 jimmywarting

That will just make my app know there's an error. The browser will show the download as successfully finished.

Menci avatar Feb 01 '20 01:02 Menci

@Menci fileStream.abort(); will do the trick

gwdp avatar Feb 04 '20 05:02 gwdp

@gwdp, it took me a while to realize also that abort wasn't what menci wanted.

When calling abort it looks like it was... well, aborted by someone. he wants to show it like if it was a network failure (like the picture shown above) I don't believe i'm handling ReadableStreamError as well as i could.

jimmywarting avatar Feb 04 '20 08:02 jimmywarting