加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 无法在Rails 3中使用send_file函数读取文件

发布时间:2020-12-16 21:18:52 所属栏目:百科 来源:网络整理
导读:我正在使用的代码: 在视图文件中: %= link_to "Download",download_image_path(image) % 在控制器中: def download @image = Image.find(params[:id]) send_file "#{RAILS_ROOT}/public" + @image.attachment.urlend 我收到一个错误: Cannot read file /
我正在使用的代码:

在视图文件中:

<%= link_to "Download",download_image_path(image) %>

在控制器中:

def download
  @image = Image.find(params[:id])
  send_file "#{RAILS_ROOT}/public"  + @image.attachment.url
end

我收到一个错误:

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:双重检查,文件存在.服务器上的相同问题,适用于所有相应控制器中的所有文件(图像,pdf,视频).

解决方法

问题是:

我在用

@image = Image.find(params[:id])
 send_file "#{RAILS_ROOT}/public"  + @image.attachment.url

它应该是

@image = Image.find(params[:id])
 send_file  @image.attachment.path

PS:确保验证图像/记录是否存在.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读