【发布时间】:2019-10-22 21:16:02
【问题描述】:
现在 Office 加载项使用新的基于 Edge 的 WebView 而不是基于 IE 11 的。我的一段代码在此更新后停止工作。我只是尝试在这里下载文件。如果我在任何浏览器中使用此代码,它可以正常工作。但是 Excel 或 Word 等 Office 应用程序使用 Microsoft Edge 的 WebView 版本。调试告诉我在这种情况下函数 window.navigator.msSaveOrOpenBlob 是未定义的。请帮我解决它。
我尝试查找有关此问题的任何文档,但没有成功。
function ClickFunc() {
var blob = new Blob(['Some Byte Array'], { type: 'application/txt' });
//output file name
var fileName = "test.txt";
//detect whether the browser is IE/Edge or another browser
//
//ERROR: In Edge WebView window.navigator.msSaveOrOpenBlob is undefined.
//
if (window.navigator && window.navigator.msSaveOrOpenBlob)
{
//To IE or Edge browser, using msSaveorOpenBlob method to download file.
window.navigator.msSaveOrOpenBlob(blob, fileName);
} else {
//To another browser, create a tag to downlad file.
//This part of code for browsers other than IE & Edge.
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
document.body.appendChild(a);
a.setAttribute('style', 'display: none');
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
a.remove();
}
}
//I use this code in HTML to call function
<button onclick="ClickFunc()">Click me</button>
window.navigator.msSaveOrOpenBlob 在 Microsoft Edge 的 WebView 中使用时未定义。
【问题讨论】:
标签: javascript webview microsoft-edge office-addins