【发布时间】: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