I am using JavaScript to download file from specific Folder.
I am not able to download the file with same extension.
It's downloading with .html
format.
How can i get rid of this issue especially in Internet Explorer
( IE
)?
Here is Javascript Function
function downloadFile(baseUrl) {
window.open(baseUrl+'/appResource/FileFormat.docx','Download');
}
Which version of IE browser are you using? I tried to test the following code in IE 11 browser, they all download the file with the same file name and extension (Unless there is a file with the same name).
window.open('/file/Document2.docx', 'Download');
and
window.open('https://file-examples.com/wp-content/uploads/2017/02/file-sample_1MB.doc', 'Download');
Try to reset the IE browser setting and check if it solve the problem.
Besides, please refer to the following code, you could also read the file to blob (or base64 string) and then using the msSaveOrOpenBlob method to download the file with file name in IE browser, and use the a tag download attribute to download the file (because IE browser doesn't support the 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();
}