問題描述:調用后端接口,接口返回文件流的形式,
第一種:window.local.href = 'xx/xx'
第二種:請求接口 接口返回一段文件流;使用 js-file-download 進行下載;
js-file-download 使用方式:
安裝
npm install js-file-download --save
使用:(在需要使用的頁面引入)
import fileDownload from 'js-file-download'
在請求是需要設置下:responseType: 'blob',
let _that=this _that.$axios.get(_that.$api.etmservice+"/export/excel/datamonitor", { responseType: 'blob', headers: { 'Content-Type': 'application/octet-stream' } }) .then(function(res){ fileDownload(res.data,'safa.xlsx') })
備注:如果不設置responseType: 'blob', 可能會出現打開文件損壞問題;
responseType: 'blob',
headers:{ 'Content-Type': 'application/json; application/octet-stream' },
這倆都指定下就好了