我能够通过以下代码将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文件,我使用的是应用程序/msword等)。
请注意,我正在从安全.net核心应用程序接口获取.doc、.docx和.xlx数据。有没有其他方法可以在浏览器中查看word、excel文件而无需下载。
发布于 2020-01-09 09:06:01
这是完全正常的行为,默认情况下你不能在浏览器中查看Word或Excel文档,这就是为什么文件只是被下载的原因。这取决于浏览器如何处理文件。旧的Internet Explorer可能也只是下载PDF,而不是显示它。
https://stackoverflow.com/questions/59660384
复制相似问题