前端:ant design vue
后端:egg.js
项目进度:现在文件能够通过前端上传至后端的public文件夹中
需求:在前端能够在线展示上传的pdf、ppt等文件
前端显示pdf、ppt等文件
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- 葬空吟 2023-05-08 11:41关注
已自行解决,后端获取文件,再使用二进制数据流传到前端,前端再转换下进行展示
后端:'use strict'; const Controller = require('egg').Controller; const fs = require('fs'); const path = require('path'); class ReadpdfController extends Controller { async getPDFFile() { const { ctx } = this; const { sendURL } = ctx.request.body; try { const filePath = path.join('app/public/', sendURL) const fileData = await new Promise((resolve, reject) => { fs.readFile(filePath, (error, data) => { if (error) { reject(error) } else { resolve(data) } }) }) this.ctx.body = fileData this.ctx.type = 'application/pdf' } catch (error) { console.log(error); ctx.body = { errCode: 1, errMsg: '数据查询错误', }; } } } module.exports = ReadpdfController;
前端:
axios.post('/api/readpdf/getPDFFile', { sendURL }, { responseType: 'blob' }) .then(response => { // 获取PDF文件数据并存储在data中 const blob = new Blob([response.data], { type: 'application/pdf' }) const data = URL.createObjectURL(blob) console.log(data, 'data'); // 打开PDF文件 window.open(data, '_blank') })
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
- ¥15 安装quartus II18.1时弹出此error,怎么解决?
- ¥15 keil官网下载psn序列号在哪
- ¥15 想用adb命令做一个通话软件,播放录音
- ¥30 Pytorch深度学习服务器跑不通问题解决?
- ¥15 部分客户订单定位有误的问题
- ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
- ¥15 Bug traq 数据包 大概什么价
- ¥15 在anaconda上pytorch和paddle paddle下载报错
- ¥25 自动填写QQ腾讯文档收集表