File Opens Instead of Downloading in Internet Explorer in a Href Link

Internet Explorer file download open allows editing of source file

Just replace

document.location = "../DUMP" + kunde + ".csv";

with

document.location = "http://yourhost/path/DUMP" + kunde + ".csv";

(you have to use http:// instead of file:// protocol to force downloading instead of opeining)

Internet Explorer is saving a TXT file as HTM when saving the target

I've run into a similar problem myself with PDF downloads, that were saved as html files, while it was still a PDF-file.
In my case I used a PHP-function to create the file and forgot to exit the code after providing the download to the frontend, which resulted in the script returning a html-result with the filetype ".pdf.html".

I don't know if it's the same case for you, but after declaring the header-data (Content-Type and Content-Disposition) let the script exit and check if that fixes the issue.

Local website: file opens in Finder instead of downloading

In order to download something, there needs to be a webserver involved. If you're just opening the .html file by double clicking on it, everything is loaded from the local file system. Since the file is already on your file system and you're linking directly to it, the file is opened directly from its location on disk. If you set up a webserver and open the site via a URL, the file will download.

Not able to download the file with same format In Internet Explorer

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();
}

Browsers try to download html file instead of opening

Likely an incorrect mime type in your .htaccess file. I suggest going into it and looking for any unwanted lines similar to the below and removing them.

AddHandler application/ etc.

and also ensure your type is set as follows:

AddType text/html .html

In order to open .htaccess in cPanel:
Click File Manager and make sure to tick Show Hidden Files (dotfiles) before clicking Go. Then the .htaccess should show up the location where wp (wp-admin, wp-content, wp-includes) is installed.

PHP: Force file download and IE, yet again

This will check for versions of IE and set headers accordingly.

// assume you have a full path to file stored in $filename
if (!is_file($filename)) {
die('The file appears to be invalid.');
}

$filepath = str_replace('\\', '/', realpath($filename));
$filesize = filesize($filepath);
$filename = substr(strrchr('/'.$filepath, '/'), 1);
$extension = strtolower(substr(strrchr($filepath, '.'), 1));

// use this unless you want to find the mime type based on extension
$mime = array('application/octet-stream');

header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.sprintf('%d', $filesize));
header('Expires: 0');

// check for IE only headers
if (preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']) || (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false)) {
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
} else {
header('Pragma: no-cache');
}

$handle = fopen($filepath, 'rb');
fpassthru($handle);
fclose($handle);

How to write html code for download file supporting IE?

<form method="get" action="input.pdf">
<button type="submit">Download</button>
</form>

You could use a button like this for the download.
This wont work in files like txt and images. for forcing download for these files, changes need to made in the server side.



Related Topics



Leave a reply



Submit