【问题标题】:To convert blob into .doc, .docx,.xls or .txt to view in browser without download using Javascript将 blob 转换为 .doc、.docx、.xls 或 .txt 以在浏览器中查看而无需使用 Javascript 下载
【发布时间】:2020-04-26 20:19:26
【问题描述】:

我可以通过以下代码将 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);
        }

这适用于 chrome 和 firefox 中的 pdf。但是对于 .doc、.docx、.xlx,虽然我提供了适当的 mime 类型,但它正在下载。 (即对于 .doc 文件,我使用的是 application/msword 等)。

请注意,我正在从安全的 .net 核心 api 获取 .doc、.docx、.xlx 数据。有没有其他方法可以在浏览器中查看word,excel文件而无需下载。

【问题讨论】:

  • 这取决于浏览器。浏览器本身并不显示 docxs 或 xlxs,甚至 pdf。如果安装了用于显示这些文件的适当扩展并且处于活动状态,则所述文件将显示在浏览器中。

标签: javascript c# file


【解决方案1】:

这是完全正常的行为,默认情况下您无法在浏览器中查看 Word 或 Excel 文档,这就是文件正在下载的原因。这取决于浏览器如何处理文件。旧的 Internet Explorer 也可能只是下载 PDF 而不是显示它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多