目录

通过后端文件流二进制,前端在线预览pdf,docx

目录

通过后端文件流二进制,前端在线预览pdf,docx

小白,如果有什么错误欢迎评论指正哈。

pdf:

获取到的文件流转为blob,然后this.url就可以直接赋值给iframe的src了。

  let blob=new Blob([res],{type:"application/pdf"})
        let url=URL.createObjectURL(blob)
         this.url=url

之前是直接给iframe的src赋值的链接地址,因为后端的接口也没有设置token,但是本地运行是ok,部署之后就不行了,不清楚是啥问题,可能跨域?maybe idont know

然后docx用的mammoth将word文档转为html格式,看到有人说doc好像就不行,我没试过。

但是docx可以

import mammoth from “mammoth”;//引入

 let _this=this
   mammoth.convertToHtml({ arrayBuffer: res })
              .then((resultObject)=>{
                            _this.vHtml = resultObject.value;
                        })
                        .done();

res就是文件流哈。

_this.vHtml就是标签中v-html绑定的值。也可以获取到标签用innerHtml赋值.