I have a link_to Rails helper that downloads a wallpaper when clicked. But the image is loading in the browser instead of being downloaded immediately.
<%= link_to "1920x1080", @download.wallpapers[1].wallpaper.url %>
But at the same time I have a link_to Rails helper that downloads a screensaver of .exe format but here it works as inteded: file being downloaded immediately.
<%= link_to "720p", @download.screensavers.first.screensaver.url %>
What should I add or do so that the images will not be opened in the browser but instead be downloaded immediately?
Thanks!
There is an easier way to do this with the HTML5 download attribute.
Here's a simple solution using the HTML5 download attribute with paperclip
Rails 3 / 4:
in routes:
in controller:
in view:
Generally, the cleanest way to do this is to set the appropriate header when sending the image:
The send_file method will allow you to set this header appropriately if you're serving the file from the filesystem:
http://api.rubyonrails.org/classes/ActionController/Streaming.html#method-i-send_file
If the file is stored in your database, you can use send_data instead:
http://api.rubyonrails.org/classes/ActionController/Streaming.html#method-i-send_data
Instead of putting the link of the image in your tag, you can handle it in your controller. And then in your controller you can do something like
Read this