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

问题描述 投票:0回答:1

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

这里是Javascript函数

function downloadFile(baseUrl) {
    window.open(baseUrl+'/appResource/FileFormat.docx','Download');
}
javascript google-chrome internet-explorer opera mozilla
1个回答
0
投票

您正在使用哪个版本的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浏览器中下载具有文件名的文件,并使用标签下载属性进行下载文件(因为IE浏览器不支持download attribute)。

  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();
    }
© www.soinside.com 2019 - 2024. All rights reserved.