预览.pdf .doc .xlsx文档

读取接口访问的url,预览文档的内容

onClick={async () => {
              if (record.fileUrl) {
                const res = await viewDirectory(record.fileUrl);
                if (record.fileUrl.includes('.pdf')) {
                  const binaryData: any = [];
                  binaryData.push(res);
                  const url = window.URL.createObjectURL(res);
                 window.location.href = url;
                } else if (record.fileUrl.includes('.docx')) {
                  // eslint-disable-next-line @typescript-eslint/no-use-before-define
                  showModal();
                  renderAsync(res, childRef.current, null, {
                    className: 'kaimo-docx-666', // string:默认和文档样式类的类名/前缀
                    inWrapper: true, // boolean:启用围绕文档内容的包装器渲染
                    ignoreWidth: false, // boolean:禁用页面的渲染宽度
                    ignoreHeight: false, // boolean:禁止渲染页面高度
                    ignoreFonts: false, // boolean:禁用字体渲染
                    breakPages: true, // boolean:在分页符上启用分页
                    ignoreLastRenderedPageBreak: true, // boolean:在 lastRenderedPageBreak 元素上禁用分页
                    experimental: false, // boolean:启用实验功能(制表符停止计算)
                    trimXmlDeclaration: true, // boolean:如果为true,解析前会从​​ xmlTemplate 文档中移除 xmlTemplate 声明
                    useBase64URL: false, // boolean:如果为true,图片、字体等会转为base 64 URL,否则使用URL.createObjectURL
                    useMathMLPolyfill: false, // boolean:包括用于 chrome、edge 等的 MathML polyfill。
                    showChanges: false, // boolean:启用文档更改的实验性渲染(插入/删除)
                    debug: false, // boolean:启用额外的日志记录
                  }).then((ress) => {
                    // eslint-disable-next-line @typescript-eslint/no-use-before-define
                    console.log('res---->', ress);
                  });
                } else if (record.fileUrl.includes('.xlsx')) {
                  <FileViewer
                    fileType={'xlsx'} //文件类型
                    filePath={res} //文件地址
                    onError={(e: any) => {
                      console.log('错误信息', e);
                    }}
                    errorComponent={console.log('发生错误')}
                    unsupportedComponent={message.error('解析失败,请下载后查看')}
                  />;
                }
              } else {
                message.error('不存在文件');
              }
            }}

结果:word文档内容

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值