前端接收pdf文件流实现预览pdf文件

const previewPdf = () => {
    axios({
      method: 'get', // 请求方式
      responseType: 'blob',
      url: '/afa/pdfxxx', // 请求路径
    }).then((res = {}) => {
      if (`${res.status}` === '200') {
        if (window.navigator.msSaveBlob) { // IE 
        //IE无法打开Blob URL链接,所以不能预览只能通过window.navigator.msSaveBlob下载
        //注:msSaveBlob的第二个参数要有.pdf后缀,不然IE下载后是没有后缀的文件
          const blob = new window.Blob([res.data], { type: 'application/pdf;charset-UTF-8' });
          window.navigator.msSaveBlob(blob, `${filename}.pdf`);
        } else {
          const blob = new window.Blob([res.data], {
            type: 'application/pdf;charset-UTF-8',
          });
          const href = URL.createObjectURL(blob);
          window.open(href);
        }
      }
    });
  };

如果没有自动打开预览,可能是这个地方拦截了,点击它将拦截关闭


image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容