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' }) }

浙公網安備 33010602011771號