Force to open “Save As…” popup open at text link c

2018-12-31 19:57发布

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 the "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 a file as below, it just links to file and is trying to open the file.

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

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 a download link...

17条回答
余生无你
2楼-- · 2018-12-31 20:37
<a href="file link" download target="_blank">Click here to download</a>

It works for me in Firefox and Chrome.

查看更多
骚的不知所云
3楼-- · 2018-12-31 20:39

I had this same issue and found a solution that has worked great so far. You put the following code in your .htaccess file:

<FilesMatch "\.(?i:pdf)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

It came from Force a File to Download Instead of Showing Up in the Browser.

查看更多
呛了眼睛熬了心
4楼-- · 2018-12-31 20:39

Just put the below code in your .htaccess file:

AddType application/octet-stream .csv
AddType application/octet-stream .xls
AddType application/octet-stream .doc
AddType application/octet-stream .avi
AddType application/octet-stream .mpg
AddType application/octet-stream .mov
AddType application/octet-stream .pdf

Or you can also do trick by JavaScript

element.setAttribute( 'download', whatever_string_you_want);
查看更多
旧人旧事旧时光
5楼-- · 2018-12-31 20:40

With large PDF files the browser hangs. In Mozilla, menu ToolsOptionsApplications, then next to the content type Adobe Acrobat document. In the Action drop down, select Always ask.

This did not work for me, so what worked was:

Menu Tools* → Add-onsAdobe Acrobat (Adobe PDF plugin for Firefox) → DISABLE. Now I am able to download e-books!

查看更多
若你有天会懂
6楼-- · 2018-12-31 20:41

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 to decide what do to with the content you provide. The user can always force their browser to download the file if they wish to.

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');  // Allow support for download resume
header('Content-Length: ' . filesize($path));  // File size
header('Content-Encoding: none');
header('Content-Type: application/pdf');  // Change the mime type if the file is not PDF
header('Content-Disposition: attachment; filename=' . $filename);  // Make the browser display the Save As dialog
readfile($path);  // This is necessary in order to get it to actually download the file, otherwise it will be 0Kb

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

查看更多
旧时光的记忆
7楼-- · 2018-12-31 20:41

I just used this, but I don't know if it works across all browsers.

It works in Firefox:

<a href="myfile.pdf" download>Click to Download</a>
查看更多
登录 后发表回答