Kiril Spasov
Kiril Spasov

Reputation: 99

Download file, not load on browser

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

Answers (2)

Tim Zimmermann
Tim Zimmermann

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

HTML5 download attribute:

<a href="www.abcd.com/manual.pdf" download>Download File</a>

This opens a save as dialog

Upvotes: 2

Related Questions