我在html.erb中使用了以下标记来显示和下载一个不在公共/图像文件夹中的jpg文件:
<%= image_tag retrieve_photo_path(@photo) %>
<%= link_to "Download Photo", download_photo_path(@photo) %>
我的控制器代码看起来像这样:
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
下载链接运行良好,但图像标记显示一个红色的x(损坏的图像)。我遗漏了什么?我在WinXP上使用InstantRails,更新到了Rails 2.3.4和Ruby1.8.6。
发布于 2009-11-09 09:17:11
您没有正确读取文件数据,您需要先打开文件。
按如下方式修改您的retrieve
操作:
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
https://stackoverflow.com/questions/1698386
复制相似问题