接口调用拿到pdf的文件流,用blob处理这个文件流拿到url,使用window.open跳转新的窗口进行预览
api({dataType: 'blob',
}).then(res =>{if(res.code === 0){this.previewPDF(res,'application/pdf;charset=utf-8','pdf文件名')}
})previewPDF (res, type, fname) {try {if(!res|| !res.data || !res.headers) {throw new Error("Invalid response")}const fileType = res.headers['content-type'] || typeconsole.log(res.headers['content-type']) const blob = new Blob([res.data], {type: fileType})console.log(blob) const urlObject = URL.createObjectURL(blob)console.log(urlObject ) window.open(urlObject, '_blank');}catch(err) {throw new Error("预览失败")}
}
接口返回的数据
打印res.headers['content-type']
响应头
请求头
打印blob
打印urlObject
文件预览