Rails 文件的安全下载,报 Cannot read file

ghn645568344 · 2016年10月21日 · 最后由 ghn645568344 回复于 2016年10月21日 · 2037 次阅读

RT 根据《Nginx + Rails 下如何进行文件的安全下载?https://ruby-china.org/topics/28951》学习文件安全下载 现在已经开始用 Nginx 传到逻辑处理,但用 send_file 传递时

因为没法使用File.find   这个方法、  报没有这个方法,所以直接把路径弄在send_file 上了
send_file "public/uploads/base_user/idcard_photo/#{params[:id]}/#{params[:name]}", type: "image/jpg", disposition: 'inline'

报错:

ctionController::MissingFile (Cannot read file public/uploads/base_user/idcard_photo/9424/image-1477017844236)

可以确定是有这个文件的,且路径正确 请问大家这个该怎么弄?已经把图片的权限改成 777 也不好用,网上也没查出什么,请教大家了

换成绝对路径试试: File.join(Rails.root, "public/uploads/base_user/idcard_photo/#{params[:id]}/#{params[:name]}")

那个 File 只表示存文件的模型,碰巧同名而已。

#1 楼 @flingfox63 😍 万分感谢 原因 获取参数的时候 url 中带点 " .jpg" 获取 name 参数时 没有获得到 " .jpg" 信息,加上后就好了。。。。😱 请问 怎么能直接获得到 点 的参数呢?

#1 楼 @flingfox63 谢谢了 已经解决了

resources :maps do
  collection do
    get :tag, constraints: { id: /[\w\d\.\-\_]+/i }
  end
end
ghn645568344 关闭了讨论。 10月24日 13:16
需要 登录 后方可回复, 如果你还没有账号请 注册新账号