可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am giving link of a pdf file on my web page for download, like below
<a href=\"myfile.pdf\">Download Brochure</a>
The problem is when user clicks on this link then
- If the user have installed Adobe Acrobat, then it opens the file in the same browser window in Adobe Reader.
- If the Adobe Acrobat is not installed then it pop-up to the user for Downloading the file.
But I want it always pop-up to the user for download, irrespective of \"Adobe acrobat\" is installed or not.
Please tell me how i can do this?
回答1:
Instead of linking to the .PDF file, instead do something like
<a href=\"pdf_server.php?file=pdffilename\">Download my eBook</a>
which outputs a custom header, opens the PDF (binary safe) and prints the data to the user\'s browser, then they can choose to save the PDF despite their browser settings. The pdf_server.php should look like this:
header(\"Content-Type: application/octet-stream\");
$file = $_GET[\"file\"] .\".pdf\";
header(\"Content-Disposition: attachment; filename=\" . urlencode($file));
header(\"Content-Type: application/octet-stream\");
header(\"Content-Type: application/download\");
header(\"Content-Description: File Transfer\");
header(\"Content-Length: \" . filesize($file));
flush(); // this doesn\'t really matter.
$fp = fopen($file, \"r\");
while (!feof($fp))
{
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
fclose($fp);
PS: and obviously run some sanity checks on the \"file\" variable to prevent people from stealing your files such as don\'t accept file extensions, deny slashes, add .pdf to the value
回答2:
This is a common issue but few people know there\'s a simple HTML 5 solution:
<a href=\"./directory/yourfile.pdf\" download=\"newfilename\">Download the pdf</a>
Where newfilename
is the suggested filename for the user to save the file. Or it will default to the filename on the serverside if you leave it empty, like this:
<a href=\"./directory/yourfile.pdf\" download>Download the pdf</a>
Compatibility: I tested this on Firefox 21 and Iron, both worked fine. It might not work on HTML5-incompatible or outdated browsers. The only browser I tested that didn\'t force download is IE...
Check compatibility here: http://caniuse.com/#feat=download
回答3:
Don\'t loop through every file line.
Use readfile instead, its faster. This is off the php site:
http://php.net/manual/en/function.readfile.php
$file = $_GET[\"file\"];
if (file_exists($file)) {
header(\'Content-Description: File Transfer\');
header(\'Content-Type: application/octet-stream\');
header(\"Content-Type: application/force-download\");
header(\'Content-Disposition: attachment; filename=\' . urlencode(basename($file)));
// header(\'Content-Transfer-Encoding: binary\');
header(\'Expires: 0\');
header(\'Cache-Control: must-revalidate, post-check=0, pre-check=0\');
header(\'Pragma: public\');
header(\'Content-Length: \' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
Make sure to sanitize your get variable as someone could download some php files...
回答4:
Instead of using a PHP script, to read and flush the file, it\'s more neat to rewrite the header using .htaccess
. This will keep a \"nice\" URL (myfile.pdf
instead of download.php?myfile
).
<FilesMatch \"\\.pdf$\">
ForceType applicaton/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
回答5:
I found a way to do it with plain old HTML and JavaScript/jQuery that degrades gracefully. Tested in IE7-10, Safari, Chrome, and FF:
HTML for download link:
<p>Thanks for downloading! If your download doesn\'t start shortly,
<a id=\"downloadLink\" href=\"...yourpdf.pdf\" target=\"_blank\"
type=\"application/octet-stream\" download=\"yourpdf.pdf\">click here</a>.</p>
jQuery (pure JavaScript code would be more verbose) that simulates clicking on link after a small delay:
var delay = 3000;
window.setTimeout(function(){$(\'#downloadLink\')[0].click();},delay);
To make this more robust you could add HTML5 feature detection and if it\'s not there then use window.open()
to open a new window with the file.
回答6:
This is the key:
header(\"Content-Type: application/octet-stream\");
Content-type application/x-pdf-document or application/pdf is sent while sending PDF file. Adobe Reader usually sets the handler for this MIME type so browser will pass the document to Adobe Reader when any of PDF MIME types is received.
回答7:
There is an easier way in HTML5: Add a Download
attribute.
It is supported by most of the modern browsers.
<a download href=\"file.pdf\">Download PDF</a>
回答8:
I know I am very late to answer this but I found a hack to do this in javascript.
function downloadFile(src){
var link=document.createElement(\'a\');
document.body.appendChild(link);
link.href= src;
link.download = \'\';
link.click();
}
回答9:
Try this:
<a href=\"pdf_server_with_path.php?file=pdffilename&path=http://myurl.com/mypath/\">Download my eBook</a>
The code inside pdf_server_with_path.php is:
header(\"Content-Type: application/octet-stream\");
$file = $_GET[\"file\"] .\".pdf\";
$path = $_GET[\"path\"];
$fullfile = $path.$file;
header(\"Content-Disposition: attachment; filename=\" . Urlencode($file));
header(\"Content-Type: application/force-download\");
header(\"Content-Type: application/octet-stream\");
header(\"Content-Type: application/download\");
header(\"Content-Description: File Transfer\");
header(\"Content-Length: \" . Filesize($fullfile));
flush(); // this doesn\'t really matter.
$fp = fopen($fullfile, \"r\");
while (!feof($fp))
{
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
fclose($fp);
回答10:
Here\'s a different approach. I prefer rather than to rely on browser support, or address this at the application layer, to use web server logic.
If you are using Apache, and can put an .htaccess file in the relevant directory you could use the code below. Of course, you could put this in httpd.conf as well, if you have access to that.
<FilesMatch \"\\.(?i:pdf)$\">
Header set Content-Disposition attachment
</FilesMatch>
The FilesMatch directive is just a regex so it could be set as granularly as you want, or you could add in other extensions.
The Header line does the same thing as the first line in the PHP scripts above. If you need to set the Content-Type lines as well, you could do so in the same manner, but I haven\'t found that necessary.
回答11:
I solved mine using the whole url of the PDF file (Instead of just putting the file name or location to href):
a href=\"domain . com/pdf/filename.pdf\"
回答12:
In a Ruby on Rails application (especially with something like the Prawn gem and the Prawnto Rails plugin), you can accomplish this a little more simply than a full on script (like the previous PHP example).
In your controller:
def index
respond_to do |format|
format.html # Your HTML view
format.pdf { render :layout => false }
end
end
The render :layout => false part tells the browser to open up the \"Would you like to download this file?\" prompt instead of attempting to render the PDF. Then you would be able to link to the file normally: http://mysite.com/myawesomepdf.pdf