下载流文件
接口返回类型设置为blob
export function goodsCompareReport(params) { let data = JSON.stringify(params); return request({ url: `/api/v1/componentGoods/compare/report`, method: 'post', data, headers: { 'Content-Type': 'application/json', }, responseType: 'blob', }) }
downloadBlob(res) { let blob = new Blob([res.data]) let downloadElement = document.createElement('a') let href = window.URL.createObjectURL(blob); //创建下载的链接 downloadElement.href = href; downloadElement.download = `new name.xlsx`; //下载后文件名 document.body.appendChild(downloadElement); downloadElement.click(); //点击下载 document.body.removeChild(downloadElement); //下载完成移除元素 window.URL.revokeObjectURL(href); //释放blob对象 },
下载普通文本
downlodTxt() { let a = document.createElement('a'); a.href = 'data:text/plain;charset=utf-8,' + this.ibs; // 文件内容 a.download = `123.txt`; // 文件名 document.body.appendChild(a); a.click(); document.body.removeChild(a); },