在开发中遇到ZIP 压缩包文件下载的需要,在此记录一下。
我这里是有后端返回 ZIP 的流文件。才用了如下办法解决。
this.$api.gameApi.uploadBundles(this.gameInformation.id,row.id).then(res => { const blob = new Blob([res.data], {type: 'application/zip'}) const url = window.URL.createObjectURL(blob) const link = document.createElement('a') // 创立a标签 link.href = url link.download = row.name // 重命名文件 link.click() URL.revokeObjectURL(url) // 开释内存 })
单纯这么应用,下载下来的压缩包打不开,须要对 axios
进行如下配置
uploadBundles(id, bundlesId) { return $axios({ methods: "get", url: `/v1/${id}/material/bundle/${bundlesId}`, responseType: 'blob' }) },
增加: responseType: 'blob'
即可。