Reputation: 43929
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.
Upvotes: 7
Views: 8225
Reputation: 31
Just change URL method by path:
send_file @image.attachment.path # this is the right way!.
Upvotes: 3
Reputation: 184
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.
Upvotes: 1
Reputation: 43929
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.
Upvotes: 16
Reputation: 4176
Does this ?1320582022
belong to the filename? I'm not sure about the white spaces in the filename either, maybe they need to be escaped.
Upvotes: 0