[英]To convert blob into .doc, .docx,.xls or .txt to view in browser without download using Javascript
我可以通过以下代码将 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文件而无需下载。
这是完全正常的行为,默认情况下您无法在浏览器中查看 Word 或 Excel 文档,这就是刚刚下载文件的原因。 这取决于浏览器如何处理文件。 旧的 Internet Explorer 也可能只是下载 PDF 而不是显示它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.