里面pdf下载,不是预览
0 条评论vue 里面pdf下载,不是预览
handleFileDownload: function (url, filename) {
let a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
},
handlePdfLink: function (filename, url) {
filename = filename+".pdf"
let that=this
fetch(url, {
method: 'get',
responseType: 'arraybuffer',
})
.then(function (res) {
if (res.status !== 200) {
return res.json()
}
return res.arrayBuffer()
})
.then((blobRes) => {
// 生成 Blob 对象,设置 type 等信息
const e = new Blob([blobRes], {
type: 'application/octet-stream',
'Content-Disposition': 'attachment'
})
// 将 Blob 对象转为 url
const link = window.URL.createObjectURL(e)
that.handleFileDownload(link, filename)
}).catch(err => {
console.error(err)
})
},