当图片隐藏在Controller后面时,如何在浏览器中打开图片而不是下载图片?

问题描述

| 当图片位于公共区域时,例如
/public/temp
,单击以下缩略图可打开原始图像(在同一窗口中):
<a href=\"/temp/original.jpg\">
  <img src=\"/temp/thumb.jpg\" />
</a>
但是,当图片隐藏在Controller后面时,HTML如下所示:
<a href=\"/assets/1/original\">
  <img src=\"/assets/1/thumb\" />
</a>
单击缩略图会将原始图片下载到我的计算机。 在这种情况下,是否可以强制在浏览器中打开原始图片(就像图片在公共区域中一样)? 以下是相关代码
class AssetsController < ApplicationController
  def download
    head(:not_found) and return unless file = Asset.find_by_id(params[:id])
    path = file.asset.path(params[:style])
    head(:bad_request) and return unless File.exist?(path)
    send_file(path,{ :type => File.mime_type?(path) })
  end    
end
mime_type?
来自
mimetype_fu
宝石)
# config/routes.rb
match \"/assets/:id/:style\" => \"assets#download\"
    

解决方法

        我找到了答案:
send_file(path,{ :type => File.mime_type?(path),:disposition => \'inline\' })