一、打印文件
// 報表打印
handlePdf(row) {wayAPI(row.billcode).then((res) => {var binaryData = [];binaryData.push(res);let url = window.URL.createObjectURL(new Blob(binaryData, {type: "application/pdf"})); window.open("/static/pdf/web/viewer.html?file=" + encodeURIComponent(url));});
},
二、下載文件,當返回的是文件路徑時候(比如:/file/文件.pdf),直接用this.download()方法
waysAPI().then((response) => {this.download(response.data);//response.data是返回的路徑
});
//這里的waysAPI是已經封裝好了請求
三、下載文件,當返回是文件流的形式時候(文件流)
excels(qparams).then(response => { //qparams是接口參數const blob = new Blob([response], {type: "application/vnd.ms-excel;charset=utf-8"});const fileName = '下載的表格名稱' + '.xlsx';if (window.navigator.msSaveOrOpenBlob) {navigator.msSaveBlob(blob, fileName);} else {const link = document.createElement("a");link.href = window.URL.createObjectURL(blob);link.download = fileName;link.click();window.URL.revokeObjectURL(link.href);}
});
上面是excel,如果是pdf,那么
const blob = new Blob([res.data], {type: 'application/pdf'});
api.js文件中
// 導出入庫單
export function excels(query) {return request({url: 'urls',method: 'get',params: query,responseType: 'blob' //這個必須,不然會亂碼})
}