How to force a Download File prompt instead of displaying it in-browser with HTML?
This is something that you cannot absolutely control with HTML itself.
If the user is having a browser with PDF reading capabilities (or a plugin) and the corresponding settings to open PDF files in-browser, the PDF will open like that.
The PDF opens in a new tab simple because of your target="_blank"
, which has nothing to do with a download prompt.
If you are using HTML5 you can use the download attribute:
<a href="sample.pdf" download="sample.pdf">Download</a>
If you have a back-end service which you can control or you feel like fiddling with your Web Server, you can always look for setting the right Content-Disposition
. See this SO question for some nice discussion on Content-Disposition.
Force browser to download file instead of opening it
You just need to make sure to send these headers:
Content-Disposition: attachment; filename=song.mp3;
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
The send_file
method does it for you:
get '/:file' do |file|
file = File.join('/some/path', file)
send_file(file, :disposition => 'attachment', :filename => File.basename(file))
end
Can I force the browser to download the PDF file instead of opening it?
If you want the browser to download as attachment, you need to say so using the Content-Disposition header field. See http://greenbytes.de/tech/webdav/rfc6266.html#disposition.type, disposition type "attachment".
(HTML) Download a PDF file instead of opening them in browser when clicked
There is now the HTML 5 download
attribute that can handle this.
I agree, and think Sarim's answer is good (it probably should be the chosen answer if the OP ever returns). However, this answer is still the reliable way to handle it (as Yiğit Yener's answer points out and--oddly--people agree with). While the download attribute has gained support, it's still spotty:
http://caniuse.com/#feat=download
force browser to download a file instead viewing it
You need to force file download in your server-side script:
Here is a PHP example:
$file = "path/to/my-file.pdf";
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
Force browser to download PDF document instead of opening it
You should look at the "Content-Disposition" header; for example setting "Content-Disposition" to "attachment; filename=foo.pdf" will prompt the user (typically) with a "Save as: foo.pdf" dialog, rather than opening it. This, however, needs to come from the request that is doing the download, so you can't do this during a redirect. However, ASP.NET offers Response.TransmitFile
for this purpose. For example (assuming you aren't using MVC, which has other preferred options):
Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=foo.pdf");
Response.TransmitFile(filePath);
Response.End();
Related Topics
Descending Sort by Value of a Hash in Ruby
How to Remove Installed Ri and Rdoc
When (If) to Consolidate Activerecord Migrations
How to Overwrite a Printed Line in the Shell with Ruby
How to Add to a Serialized Array
Ruby Ternary Operator Without Else
Ruby Inject with Initial Being a Hash
Ruby 1.9 - Invalid Multibyte Char (Us-Ascii)
How to Enable Tls V1.2 in Ruby? If So, How
How to Write a Ruby Switch Statement (Case...When) with Regex and Backreferences
Format the Date Using Ruby on Rails
Add Image to Layout in Ruby on Rails
How to Set the Socket Timeout in Ruby