简体   繁体   English

将 blob 转换为 .doc、.docx、.xls 或 .txt 以在浏览器中查看,无需使用 Javascript 下载

[英]To convert blob into .doc, .docx,.xls or .txt to view in browser without download using Javascript

I am able to convert blob into pdf attachment to view in browser without download by following code我可以通过以下代码将 blob 转换为 pdf 附件以在浏览器中查看而无需下载

 var ieEDGE = navigator.userAgent.match(/Edge/g);
        var ie = navigator.userAgent.match(/.NET/g); // IE 11+
        var oldIE = navigator.userAgent.match(/MSIE/g);
        //var bytes = new Uint8Array(response); //use this if data is raw bytes else directly pass resData
        var blob = new window.Blob([response], { type: 'application/pdf' });

        if (ie || oldIE || ieEDGE) {
          window.navigator.msSaveBlob(blob, fileName);
        }
        else {
          var fileURL = URL.createObjectURL(blob);
          window.open(fileURL);
        }

This is working for pdf in chrome and firefox.这适用于 chrome 和 firefox 中的 pdf。 But for .doc, .docx,.xlx, it is downloading though i am providing appropriate mime type.但是对于 .doc、.docx、.xlx,虽然我提供了适当的 mime 类型,但它正在下载。 ( ie for .doc file I am using application/msword and so on). (即对于 .doc 文件,我正在使用 application/msword 等)。

Note that I am fetching .doc,.docx,.xlx data from secure .net core api.请注意,我正在从安全的 .net 核心 api 获取 .doc、.docx、.xlx 数据。 Is there any other way to view word, excel file in browser without download.有没有其他方法可以在浏览器中查看word、excel文件而无需下载。

This is totally normal behavior, you can't view Word or Excel Documents in your browser by default, that's why the file is just being downloaded.这是完全正常的行为,默认情况下您无法在浏览器中查看 Word 或 Excel 文档,这就是刚刚下载文件的原因。 It depends on the browser how files are treated.这取决于浏览器如何处理文件。 Old Internet Explorer might just download the PDF too instead of showing it.旧的 Internet Explorer 也可能只是下载 PDF 而不是显示它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM