jimmywarting / StreamSaver.js

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

There is something wrong with the file saved in firefox. #334

Open haoma2514 opened 8 months ago

haoma2514 commented 8 months ago

The zip file saved by firefox is incomplete. The following are files saved by Firefox: 图片 The file saved by chrome is correct. 图片

this's my code:

export const downloadFileByFetchToZip = (fileName:string,files:downloadAnnexModel[])=>{   
    streamSaver.mitm= '/shtml/mitm.html'    
    const zipFileOutputStream = streamSaver.createWriteStream(fileName+".zip");      
    const fileIterator = files.values();  
    const readableZipStream = new Zip({  
        async pull(ctrl) {  
            const fileInfo = fileIterator.next();  
            if (fileInfo.done) { 
                ctrl.close();  
            } else {  
                const {fileName, fileUrl} = fileInfo.value;  
                return fetch(fileUrl).then(res => {  
                    ctrl.enqueue({  
                        fileName,  
                        stream: () => res.body  
                    });  
                }).catch((x)=>console.error("error",x))  
            }      
            //ctrl.close()   
        }    
    });  
    if (window.WritableStream && readableZipStream.pipeTo) {  
        readableZipStream  
            .pipeTo(zipFileOutputStream)  
            .then(() => console.log("done")).catch((error)=>{
                console.log("error",error)
            })  
    }  
}

After the file is saved, the then block is not executed, and the catch block is not executed. It is not clear which link has gone wrong.