文件下载 blob流 记录

文件流:


image.png

通过下面代码下载文件后


image.png
api:
export const downloadExcel = (params) => {
  return request({
    url: '/api/mc/*******/export',
    method: 'get',
    params: params
  })
}
js:
handleDownload2() {
        var params = Object.assign({},this.query)
        downloadExcel(params).then((res)=>{
          console.log(res)
          this.downfile('乡镇船舶基础信息表', res.data)
        })
        // window.open(`/api/mc/townshipshipinfo/export?${this.website.tokenHeader}=${getToken()}&shipName=${this.query.shipName}&shipDistrict=${this.query.shipDistrict}&shipOwnerName=${this.query.shipOwnerName}&portId=${this.query.portId}&moorage=${this.query.moorage}`);
      },
      downfile (name, data) {
        let fileName = name
        let blob = new Blob([data], { type: 'application/vnd.ms-excel;charset=UTF-8' })
        const a = document.createElement('a')
        // 创建URL
        const blobUrl = window.URL.createObjectURL(blob)
        a.download = fileName
        a.href = blobUrl
        document.body.appendChild(a)
        // 下载文件
        a.click()
        // 释放内存
        URL.revokeObjectURL(blobUrl)
        document.body.removeChild(a)
      },

原因:

请求数据的时候没有加请求数据类型,导致请求到的数据错误

正确的是

请求接口 responseType: 'blob',

api:
export const downloadExcel = (params) => {
  return request({
    url: '/api/mc/*******/export',
    method: 'get',
    responseType: 'blob',
    params: params
  })
}

正确的数据类型:


image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容