vue下载文件流

主要针对后台返回的文件流进行处理:js文件

export function download (data,titName) {
    if(!data){
      return
    }
    const content = data
    const blob = new Blob([content],{type: "application/vnd.ms-excel"})
    const fileName = titName?titName: ''
    if ('download' in document.createElement('a')) { // 非IE下载
      const elink = document.createElement('a')
      elink.download = fileName
      elink.style.display = 'none'
      elink.href = URL.createObjectURL(blob)
      document.body.appendChild(elink)
      elink.click()
      URL.revokeObjectURL(elink.href) // 释放URL 对象
      document.body.removeChild(elink)
    } else { // IE10+下载
      navigator.msSaveBlob(blob, fileName)
    }
  }
import { download } from "@/utils/index";    引入

请求接口时必加 “responseType: 'blob'”不然下载下来文件有问题

export function downloadFile(data) {
  return request({
    url: url,
    method: 'get',
    responseType: 'blob'
  })
}

 

posted @ 2022-05-05 15:53  前端—小白  阅读(2906)  评论(0编辑  收藏  举报