views:

35

answers:

3

Hi Friends,

I have some big size pdf catalogs at my website, and I need to link these as download. When I googled, I found such a thing noted below. It should open "Save As..." popup at link click..

 <head> 
    <meta name="content-disposition" content="inline; filename=filename.pdf">
    ...

but it doesn't work :/ when I link to file as below, it just links to file and trying to open the file.

    <a href="filename.pdf" title="Filie Name">File name</a>

Appreciate helps! thanks a lot!


UPDATE (according to answers below):

As I see there is no 100% reliable cross-browser solution for this. Probably the best way is using one of the web services listed below, and giving download link...

A: 

If you have a plugin within browser which knows how to open a pdf file it will open directly. Like in case of images and html content. So the alternative approach is not to send your mime type in response in this way browser will never know which plugin should open it. Hence it will give you a Save Open dialog box.

sushil bharwani
That's hard to achieve because it's the server that sends the mime type and when linking directly to a PDF file, you cannot change the headers.
dark_charlie
+1  A: 

Generally it happens, because some the browsers settings or plug-ins directly open pdf in a same window like an simple web-page. This might help you. I have done it in PHP few years back. But, currently I'm not working on that platform.

<?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 little snippet as a PHP file somewhere on your server and you can use it to make a file download in the browser, rather than display directly. If you want to serve files other than PDF, remove or edit line 5.

You can use it like so;

Add the following link to your HTML file. <a href="download.php?file=my_pdf_file.pdf">Download the cool PDF.</a>

Save this little snippet as a PHP file somewhere on your server and you can use it to make a file download in the browser, rather than display directly. If you want to serve files other than PDF, remove or edit line 5.

You can use it like so;

Add the following link to your HTML file. Download PDF.

Reference from : This blog

Ashay
+1  A: 

Meta tags are not a reliable way to achieve this result. Generally you shouldn't even do this - it should be left up to the user/user agent what it does with the content you provide to it. The user can certainly make its browser to download the file if he or she wishes that.
If you still want to force the browser to download the file, modify the HTTP headers directly. Here's a PHP code example:

$path = "path/to/file.pdf";
$filename = "file.pdf";
header('Content-Transfer-Encoding: binary');  // For Gecko browsers mainly
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
header('Accept-Ranges: bytes');  // For download resume
header('Content-Length: ' . filesize($path));  // File size
header('Content-Encoding: none');
header('Content-Type: application/pdf');  // Change this mime type if the file is not PDF
header('Content-Disposition: attachment; filename=' . $filename);  // Make the browser display the Save As dialog

Note that this is just an extension to the HTTP protocol and some browsers might ignore it anyway.

dark_charlie
In practice I believe this is widely implemented.
Matthew Wilson