The code I am using:
In view file:
<%= link_to "Download", download_image_path(image) %>
In controller:
def download
@image = Image.find(params[:id])
send_file "#{RAILS_ROOT}/public" + @image.attachment.url
end
I am getting an error:
Cannot read file /Users/mohit/projects/my_app/public/system/attachments/4/original/Screen Shot 2011-11-04 at 3.14.03 PM.png?1320582022
PS: double checked, the file exists. Same issue on server, for all the files (images, pdfs, videos) in all the respective controllers.
Issue was:
I was using
@image = Image.find(params[:id])
send_file "#{RAILS_ROOT}/public" + @image.attachment.url
It should be
@image = Image.find(params[:id])
send_file @image.attachment.path
PS: Make sure you validate that image/record exists.
Just change URL method by path:
send_file @image.attachment.path # this is the right way!.
I don't know how you save the image attachment url, but in general, a file name should like:
/Users/mohit/projects/my_app/public/system/attachments/4/original/Screen Shot 2011-11-04 at 3.14.03 PM.png
Notice it doesn't has "?xxxxx" in the end.
You can check your file system, whether the file name is "Screen Shot 2011-11-04 at 3.14.03 PM.png" or "Screen Shot 2011-11-04 at 3.14.03 PM.png?1320582022".
For file url, it maybe something like: http://example.com/xxx?dddd, which character "?" split the path and parameters. The string "dddd" is a parameter when request url path, it's not a part of path or filename. Parameters only support in url, not local file name.
So, I think you need to check the code of saving image attachment url, which need to exclude the parameters and only filename. And make sure the name is exactly same as the file saved to disk.
And you can try to open the file though irb directly and check the output:
>>> irb
irb> f = open('/Users/mohit/projects/my_app/public/system/attachments/4/original/Screen Shot 2011-11-04 at 3.14.03 PM.png?1320582022')
Others, try to find the error location in send_file, and check the file name.
I still can't make sure what the issue really is, just some suggestion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With