Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read file with send_file function in Rails 3

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.

like image 222
Mohit Jain Avatar asked Dec 05 '11 00:12

Mohit Jain


3 Answers

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.

like image 53
Mohit Jain Avatar answered Nov 17 '22 01:11

Mohit Jain


Just change URL method by path:

send_file  @image.attachment.path # this is the right way!.
like image 38
user2675589 Avatar answered Nov 17 '22 03:11

user2675589


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.

like image 23
Googol Avatar answered Nov 17 '22 03:11

Googol