如何使用send_file下载文件? [英] How to download file with send_file?

查看:112
本文介绍了如何使用send_file下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以启发我如何使用send_file下载文件吗?

Can someone enlighten me how can I download file with send_file?

我在app/assets/images中有一个文件image.jpg.我已经在控制器中尝试过此操作:

I have a file image.jpg inside app/assets/images. I've tried this in my controller:

def download
    send_file ("#{Rails.root}/public/images/image.jpg")
end

def download
    send_file ("#{Rails.root}/assets/images/image.jpg")
end

def download
    send_file ("#{Rails.root}/images/image.jpg")
end

def download
    send_file ("/public/images/image.jpg")
end

def download
    send_file ("/assets/public/images/image.jpg")
end

def download
    send_file ("/assets/images/image.jpg")
end

对于每个路径,它说:

ActionController::MissingFile in HomeController#download
Cannot read file 'some_path'

这可能是个问题吗?谢谢!

What could be a problem here? Thanks!

推荐答案

尝试:

IMAGES_PATH = File.join(Rails.root, "public", "images")

def download
  send_file(File.join(IMAGES_PATH, "image.jpg"))
end

这篇关于如何使用send_file下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆