记一个文件下载

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

你可能感兴趣的:(javascript)