7

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.

Jawa
  • 2,336
  • 6
  • 34
  • 39
Mohit Jain
  • 43,139
  • 57
  • 169
  • 274

4 Answers4

16

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.

Mohit Jain
  • 43,139
  • 57
  • 169
  • 274
  • if you analyze the code using brakeman , then it will show the error as MOdel attribute used as filename . just run the brakeman. its consider as send_file Image.find(params[:id]).uploaded_file.path – errakeshpd Oct 31 '14 at 13:09
3

Just change URL method by path:

send_file  @image.attachment.path # this is the right way!.
Jawa
  • 2,336
  • 6
  • 34
  • 39
1

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.

Mohit Jain
  • 43,139
  • 57
  • 169
  • 274
Googol
  • 174
  • 4
0

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.

Wukerplank
  • 4,156
  • 2
  • 28
  • 45
  • Screen Shot 2011-11-04 at 3.14.03 PM.png is the file name and ?1320582022 is coming cause i m getting issue in development mode.. – Mohit Jain Dec 05 '11 at 13:31