Vue-pdf文件流转url显示

485 阅读1分钟

在使用vue-pdf时, 我们从后端拿到的地址返回的是文件流, 此时的文件流地址是不能直接写到pdf组件作为src的, 我们需要将其转换为url地址

var url = process.env.VUE_APP_FILE_BASE_URL + `/basicFile/downFile/` + record.idFile
 
// 文件流转换
const xhr = new XMLHttpRequest()
xhr.open('GET', url, true)
xhr.responseType = 'blob'
xhr.onload = function() {
    const res = xhr.response
    // 此时, pdfPath就可以作为src使用了
    const pdfPath = window.URL.createObjectURL(res)
}
xhr.send()

最后注意跨域问题