一、打印文件
javascript
// 报表打印
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()方法
javascript
waysAPI().then((response) => {
this.download(response.data);//response.data是返回的路径
});
//这里的waysAPI是已经封装好了请求
三、下载文件,当返回是文件流的形式时候(文件流)
javascript
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,那么
javascript
const blob = new Blob([res.data], {type: 'application/pdf'});
api.js文件中
javascript
// 导出入库单
export function excels(query) {
return request({
url: 'urls',
method: 'get',
params: query,
responseType: 'blob' //这个必须,不然会乱码
})
}