简体   繁体   中英

Zip File Download in ReactJS

I want to download a Zip file when I am getting @uFÑÉ:¤µÞ(ïÀ©zTB$ðÛYæÛLQ5ôUJñP¾C.$Ì^ÖÔ#2A,é)ÂÊâð¼mÛ¡Æ'Þ±XRÀ¾Q¶/éhOêOþyås this kind of data from my api end-point. here is my code to download that zip file where I store data which I get from api end-point into generateFile:

 { this.state.generateFile ? <Button onClick={() => { const url = window.URL.createObjectURL( new Blob([this.state.generateFile], { type: "application/zip" }) ); const link = document.createElement("a"); link.href = url; link.setAttribute("saveas", "generate.zip"); document.body.appendChild(link); link.click(); this.setState({ generateFile: null }); }} >Download</Button> : null }

call your axios with responseType: 'arraybuffer'

    Axios.post('https://...',
        {
            "generation_date": this.state.generateDate,
            "merchandiser": true, "superviser": true
        },
        {
            responseType: 'arraybuffer', // add this line 
            headers: {
                Authorization: "JWT " + localStorage.getItem("token")
            }
        }).then(res =>
            this.setState({ generateFile: res.data }
            ))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM