关于javascript:记一个文件下载

6次阅读

共计 536 个字符,预计需要花费 2 分钟才能阅读完成。

在开发中遇到 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' 即可。

正文完
 0