I'm using Papeclip(2.3.1.1) with Rails(2.3.5).
In my view I write so:
<%= link_to image_tag(p.attachment.url(:small)), p.attachment.url(:original) %>,
and it becomes into
href="/system/attachments/1/original/1.JPG?1270134617 (for a tag)
src="/system/attachments/1/small/1.JPG?1270134617" (for img tag).
And when I click on the picture, my browser (Firefox) offers me to save or open picture and I want to just open picture in browser without any dialogs.
I think it's because link contains ?1270134617 after file's name. How can I fix it?
This could be related with the mime types.
Go to /config/initializers/mime_types.rb, and add (or uncomment, if it's there) this line:
Mime::Type.register "image/jpg", :jpg, ["image/jpeg", "image/pjpeg"]
Then restart your web server.
Hopefully this will make the picture "show on the browser" instead of "trying to download it".
Related
This question already has an answer here:
Force browser to download file instead of opening it
(1 answer)
Closed 5 years ago.
I have the following in my .erb:
<%= link_to 'Download PDF', planners_download_pdf_url %>
I have the following method to respond:
def download_pdf
send_file(
"#{Rails.root}/Thing/ex/example.xls",
filename: "mything.xls",
type: "application/xls"
)
end
My routes has:
get '/planners/download_pdf'
When I click my link, the method does get invoked. My problem is that is doesn't download to the browser. Instead, it takes the tab I have open in the browser and dumps the file as HTML. Like... have you ever seen an Excel file opened in notepad? It does that. See below:
How do I get this to instead download the file?
You need to add disposition: 'attachment' to your send_file option hash (or at least ensure that you do not have disposition: 'inline' set, since 'attachment' should be the default).
If this is not the issue, if you are using Turbolinks, as mentioned in other answers you need to disable Turbolinks on the link by setting data-turbolinks="false" in your link element (e.g.: data: {turbolinks: false} in your link_to tag helper).
Finally, here are some other things to try if this doesn't work:
Set type to 'application/vnd.ms-excel', the valid MIME type for XLS files.
Set the download="mything.xls" html5 attribute on the link tag directly (e.g.: download: 'mything.xls' in your link_to tag helper.
I would like the user to be able to download a file from a method I set up in a controller. Further, I don’t want the URL to change in my browser when the user downloads the file. I have this link set up
<%= link_to image_tag("cc_icon.png"), scenario_download_cc_path(subscription.scenario), target: '_blank' %>
The problem is, there is a screen blink as a new tab is spawned to generate the download. This looks visually unappealing. I have seen other sites where you click the link and something starts downloading without a blink. How do I do that?
Edit: Here is the function invoked by the link
def download_cc
scenario = Scenario.find(params[:scenario_id])
send_data scenario.cc_data, filename: "#{scenario.title}.imscc", type: 'application/zip', :disposition => 'attachment'
end
I did some local testing and my hypothesis is that Turbolinks is messing things up. I recommend that you remove target: '_blank' from the link and add data: { turbolinks: false } to opt-out of Turbolinks for this particular link. The code after changes should look like this:
<%= link_to image_tag("cc_icon.png"), scenario_download_cc_path(subscription.scenario), data: { turbolinks: false } %>
Your controller action looks good and needs no changes.
Just add a "download" attribute to your link:
<%= link_to image_tag("cc_icon.png"), scenario_download_cc_path(subscription.scenario), download: true %>
Remove target
At the moment, you are setting target="_blank", this is telling the browser that when you click the URL, you want to open a new tab. Removing this should just begin the download without opening a tab or window.
< Optional extra info below as I misread your question: >
Formatting the appearance of links
Are you using any HTML or CSS for the design of the site? If so, you can apply css state properties such as "active" "hover" "visited" "link" in your css classes.
Lets say you had a link:
My Link
If you created a css file and customised the properties for the link:
a:visited {
text-decoration: none;
}
that will allow you to alter the appearance of a visited link. You can then use any css property to customise the appearance in any way you want.
Adding CSS files to Ruby app
With Ruby, you can use: <%= stylesheet_link_tag "filename" %> to load in a css file.
Sources:
http://www.w3schools.com/css/css_link.asp
How do I use CSS with a ruby on rails application?
As long as you use send_file you should not see a screen blink.
The scenario_download_cc_path(subscription.scenario) action in your controller should look like this:
file_path = File.join(Rails.root, "/public/myfile.whatever")
send_file file_path
The behavior on Chrome and IE appears the same whether your link does or does not include target: '_blank'
I’m using Rails 5. I have set up this link for downloading a file
<%= link_to image_tag("cc_icon.png"), scenario_download_cc_path(subscription.scenario), :remote => true %>
which links to this controller method
def download_cc
scenario = Scenario.find(params[:scenario_id])
send_data scenario.cc_data, filename: "#{scenario.title}.imscc", type: 'application/zip', :disposition => 'attachment'
end
But when I click on the link above, even though I see a remote call being made, no file download appears in my browser (I’ve tried this on both Chrome and Firefox). How do I make this work? Note, removing “remote => true” is not an option because then the URL in my address bar changes, which I don’t want.
I've not seen the remote option used with file downloads. It's mostly designed for AJAX form posting and URL hits like deleting a record or action calls that make sense to do over AJAX.
If you want the link to force a "Save As..." dialog, use the download attribute in the link. A 'bare' download attribute will force the "Save As..." dialog. Its value will be the download filename. So <a href="/my/download/action" download="file_100.zip"> opens a dialog prompting you to save file_100.zip.
In my project I'm using Carrierwave to upload files like doc, and pdf.
In case I want to download a pdf my controller do
send_file #document, :disposition =>
'inline', :type => 'application/pdf'
And this works fine, displays the pdf file in browser and the url I get is /documents/file_name and it's fine too. But I want to set the "headers title" or "url title" so in the browser's tab shows the file_name instead myapp.com/documents/file_name
Is there a simple way to do that?
Unfortunately it is not possible as you describe.
The tab title is taken from the PDF documents metadata, specifically the Title attribute.
If you usually generate your document with Prawn you can do:
Prawn::Document.generate(my_filename, info: {Title: my_title})
I'm writing a service that generates PDF files from a set of XML files. The PDF is being correctly generated. However, everytime I click on the "view PDF" link, the browser asks the user to download the PDF file.
I need the PDF to display inline, just like any regular HTML page. I though I wrote the code right, but something must be missing - the browser keeps asking the user to download.
Here's the current code:
class PdfController < Controller
def generate
# stuff
send_data pdf_bytes, :disposition => 'inline', :type => 'application/pdf'
end
end
Any ideas?
Try removing the Content-Disposition header altogether. It's been my experience that Content-Disposition: attachment works pretty well, but many browsers have inconsistent behavior for any other value. If you want to display inline, it might just be better to remove the header and hope for the best. IE seems to have the most problems with this header. (Surprise, surprise.) Just make sure you're still setting Content-Type: application/pdf.
The other option would be to use an iframe and set the src of the iframe to your PDF file. Almost all browsers that support inline PDF viewing will handle this correctly. The downside is that you might end up displaying a blank iframe whereas non-supported browsers would have otherwise done a graceful fallback to simply downloading the PDF.