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

The stream download of the file is not getting closed #163

Closed renjiie closed 4 years ago

renjiie commented 4 years ago

Hi, I tried streamsaver js in my app. It can write the stream data into a file. But the download is in progress even after the stream is closed. I used the below code.

function unencrypt(value){
    // should return Uint8Array
    return new Uint8Array(value)
}

// We use fetch instead of xhr that has streaming support
fetch(url).then(res => {
    // create a writable stream + intercept a network response
    const fileStream = streamSaver.createWriteStream('filename.txt')
    const writer = fileStream.getWriter()

    // stream the response
    const reader = res.body.getReader()
    const pump = () => reader.read()
        .then(({ value, done }) => {
            let chunk = unencrypt(value)

            // Write one chunk, then get the next one
            writer.write(chunk) // returns a promise

            // While the write stream can handle the watermark,
            // read more data
            return writer.ready.then(pump)
        )

    // Start the reader
    pump().then(() =>
        console.log('Closed the stream, Done writing')
    )
})
jimmywarting commented 4 years ago

You are not closing the writable stream anywhere after you have written all the chunks to the wrtiableStream

When you are done writing, call writer.close()