Reputation: 99
I have a button on my website, and when user click is call jQuery function
event.preventDefault();
document.location.href = 'www.abcd.com/manual.pdf';
This action open 'manual.pdf' in a browser windows, but I want to download it... how to open 'SaveAs' dialog on the current OS system?
Upvotes: 1
Views: 863
Reputation: 6420
For local files, you can do this with php. The trick is to set the Content-Type and -Disposition of the HTTP response. This php-script restricts download to pdf-files.
<?php
if (isset($_GET['file'])) {
$file = $_GET['file'];
if (file_exists($file) && is_readable($file) && preg_match('/\.pdf$/',$file)) {
header('Content-type: application/pdf');
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($file);
}
} else {
header("HTTP/1.0 404 Not Found");
echo "<h1>Error 404: File Not Found: <br /><em>$file</em></h1>";
}
?>
Save this as download.php and set <a href="download.php?file=manual.pdf" ...>
.
Upvotes: 1
Reputation: 57095
<a href="www.abcd.com/manual.pdf" download>Download File</a>
This opens a save as
dialog
Upvotes: 2