I'm using the following tags in my html.erb to both display and download a jpg file that is not in the public/images folder:
<%= image_tag retrieve_photo_path(@photo) %>
<%= link_to "Download Photo", download_photo_path(@photo) %>
my controller code looks like:
def retrieve
@photo = Photo.find(params[:id])
send_data File.read(@photo.abs_filepath), :type = "image/jpeg", :disposition => "inline"
end
def download
@photo = Photo.find(params[:id])
send_file @photo.abs_filepath, :type = "image/jpeg", :filename => @photo.filename
end
The download link works perfectly, but the image tag displays a red x (broken image). What am I missing? I'm using InstantRails on WinXP, updated to Rails 2.3.4 and Ruby 1.8.6.
You're not reading the file data properly, you need to open the file first.
Modify your retrieve
action as follows:
def retrieve
@photo = Photo.find(params[:id])
File.open(@photo.abs_filepath, 'rb') do |f|
send_data f.read, :type => "image/jpeg", :disposition => "inline"
end
end
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