个人网站可以备案,xuzhou公司网站制作,网站开发外包费用会计科目,便宜正品的购物app需求
后端接口返回pdf文件流#xff0c;实现新窗口预览pdf。
解决方案
把后端返回的pdf文件流转为blob路径#xff0c;利用浏览器直接预览。
具体实现步骤
1、引入axios
import axios from axios;2、创建预览方法#xff08;具体使用时将axios的请求路径替换为你的后端…需求
后端接口返回pdf文件流实现新窗口预览pdf。
解决方案
把后端返回的pdf文件流转为blob路径利用浏览器直接预览。
具体实现步骤
1、引入axios
import axios from axios;2、创建预览方法具体使用时将axios的请求路径替换为你的后端下载地址
export async function previewFile(data: IAttachment, callback?: () void) {try {const response await axios.get(config.VITE_APP_API_URL_PREPROD /file/downloadFile, {params: {filepath: data.filePath},responseType: blob});let pdfUrl window.URL.createObjectURL(new Blob([response.data], { type: application/pdf }));window.open(pdfUrl, _blank);const newWindow window.open(pdfUrl, _blank);if (newWindow) {newWindow.onload () {newWindow.focus();};} else {// 如果新窗口被阻止提示用户ElMessage.warning($t(请允许弹出窗口以预览文件));}} catch (error) {console.error(Error preview file:, error);}
}3、在你所需要的地方调用previewFile方法
import { previewFile } from /utils;
el-button typeprimary clickpreviewFile(file)导出已选/el-button