Linking to images outside public folder

I have a project where I need to show images (.jpg's) in the browser that are outside my projects "public" directory. I'm developing on a Mac and I've tried using an absolute path "/Users/my_name/Desktop/ image.jpg". I can use this technique to read the contents of .txt files and send it as a string to the browser but I can't get images to display. Do I need to use send_file or send_data to get the images to display? Any help would be great. Thanks.

Hi Urban,

i am relatively new to Rails, so i may be wrong, but i had the same problem recently and resorted to putting smaller images in the public directory, which are displayed. The original (larger) images cannot be displayed in my app, but they can be downloaded. For downloading i used send_file.

If i am right in my assumption that there is no way to use image_tag with files outside of the app’s directory structure and it is vital for your app to do so, i would try to put the images outside and copy them to a directory beneath the public directory whenever you have to display them (and delete them afterwards). Surely, that would not improve performance…

Another thing i read of to solve the problem of having files in a public directory, but not wanting people to access them freely: Put them in directories and make those directories’ names unguessable - this way you will allow people to access single images, but they cannot access them all.

And last (but i read not much good about the method): You could put the images as blobs in the db.

Jochen

if your using the image_tag helper I thin that automatically points to your public directory. Try just using an old skool <img src="/ Users....." alt="" />

Cam

Urban,

The browser can only request things by a URI and that will be served by the web listener if it's a resource (file) under the document root. Typically, this is the public/ directory. Other resources might be available to a request being serviced by your Rails application. You could return the image *data* as a result of the request using either of the methods you mentioned:

     send_file( '/Users/my_name/Desktop/image.jpg',                 :disposition => 'inline',                 :type => 'image/jpeg',                 :stream => false,                 :filename => 'image.jpg' )

# The :filename is what will be suggested if the browser offers to save the image

     send_data(some_variable_holding_the_image_data,                :disposition => 'inline',                :type => 'image/png',                :filename => "on-the-fly.png")

The URI in the <img src='THIS'> needs to be routed to a controller action of your that finds or builds the image data and uses send_* to give it to the browser.

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

Thanks for the help everyone. I ended up using "send_file" as Rob showed above to get my project to display images in the browser that are outside my Rails project root.

UF