无法在Internet Explorer中下载相同格式的文件



我正在使用JavaScript从特定文件夹下载文件
我无法下载具有相同扩展名的文件
.html格式下载
我如何摆脱这个问题,尤其是在Internet Explorer(IE(中?

这里是Javascript函数

function downloadFile(baseUrl) {
window.open(baseUrl+'/appResource/FileFormat.docx','Download');
}

您使用的是哪个版本的IE浏览器?我试着在IE 11浏览器中测试以下代码,他们都下载了相同文件名和扩展名的文件(除非有相同名称的文件(。

window.open('/file/Document2.docx', 'Download');

window.open('https://file-examples.com/wp-content/uploads/2017/02/file-sample_1MB.doc', 'Download');

尝试重置IE浏览器设置,并检查它是否解决了问题。

此外,请参考以下代码,您也可以将文件读取到blob(或base64字符串(,然后使用msSaveOrOpenBlob方法在IE浏览器中下载带有文件名的文件,并使用a标记下载属性下载文件(因为IE浏览器不支持下载属性(。

if (window.navigator && window.navigator.msSaveOrOpenBlob) {
//IE11 and the legacy version Edge support
console.log("IE & Edge");
let blob = new Blob([data], { type: "text/html" });
window.navigator.msSaveOrOpenBlob(blob, fileName);
} else {// other browsers
console.log("Other browsers");
var bl = new Blob([data], { type: "text/html" });
var a = document.createElement("a");
a.href = URL.createObjectURL(bl);
a.download = fileName;
a.hidden = true;
document.body.appendChild(a);
a.click();
}

最新更新