直接上代码
async preview(e){
const data = {
fileName: e.fileName,
filePath: e.filePath
}
const res = await previewFile(data)
const pdfBlob = new Blob([res], { type: this.type(e.ext) });
const pdfUrl = URL.createObjectURL(pdfBlob)
const newWindow = window.open(pdfUrl, '_blank')
newWindow.onload = () => URL.revokeObjectURL(pdfUrl)
},
由于后端这里是直接将文件传给我,所以这里需要处理一下,获取blob对象,然后用type函数进行类型判断
type(type){
if(type==='.jpg'){
return 'image/jpeg'
}
if(type==='.png'){
return 'image/png'
}
if(type==='.mp4'){
return 'video/mp4'
}
if(type==='.txt'){
return 'text/plain'
}
return 'application/pdf'
},
这样就可以在新的窗口打开进行预览了