Build a link_to to download an image.
send_file
send_file method can be used in a controller to download the file in browser:
[codesyntax lang=”rails”]
send_file '/path/to/file', :type => 'image/jpeg', :disposition => 'attachment'
[/codesyntax]
But in case when you have an url for the remote file (for example, an image stored in amazon cloud storage like Amazon S3) then this code
[codesyntax lang=”rails”]
send_file 'http://someserver.com/path/to/file', :type => 'image/jpeg', :disposition => 'attachment'
[/codesyntax]
will give an error “Cannot read file http://..”.
According to this documentation send_file expects the first parameter to be the path to your file, i.e. the file should be stored locally on this server.
send_file(path, options = {})
send_data
Instead of send_file you can use send_data method as follows:
[codesyntax lang=”rails”]
require ‘open-uri’
url = ‘http://someserver.com/path/../filename.jpg’
data = open(url).read
send_data data, :disposition => ‘attachment’, :filename=>”photo.jpg”
[/codesyntax]
All code examples are avaiable here on github:
https://gist.github.com/4430975
MACMAN says:
BEAUTIFUL! THANK YOU!
MACMAN says:
Running a bit slow fetching the file on my localhost though
Ramesh Raj says:
awsome !!!
it’s working fine.