uniapp h5文件流下载pdf文件
DownLoadDoc() {
let url = 'doc/api/downloadDocFulltext?id=' + this.id //接口路径
uni.request({
url:uni.getStorageSync('baseUrl')+url, //域名拼接
responseType: 'arraybuffer', //这里记得设置响应数据格式,不然预览的pdf是空白
header: {
"GAKey": uni.getStorageSync('token') // 携带的token
},
success: (response) => {
let pdfData = response.data; //pdfData是后端返回的文件流 //创建blob数据流,并用a标签来下载
let blob = new Blob([ pdfData], {
type: 'application/pdf;charset=UTF-8'
})
var a = document.createElement('a');
document.body.appendChild(a);
a.style = 'display: none';
var url = window.URL.createObjectURL(blob);
a.href = url;
a.download = `${this.title}.pdf`; // 文件标题
a.click();
a.remove();
window.URL.revokeObjectURL(url);
}
})
}