如何使用send_file下载文件?

有人可以告诉我如何使用send_file下载文件?

我在app/assets/images有一个文件image.jpg 。 我在我的控制器中试过这个:

 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' 

这可能是个问题? 谢谢!

尝试:

 IMAGES_PATH = File.join(Rails.root, "public", "images") def download send_file(File.join(IMAGES_PATH, "image.jpg")) end 
 In your view => <%= link_to "click here to download", signed_feeds_pdf_path(:feed_image_path => feed_image.feedimage.path), target: '_self' %> In your controller => def pdf file_name = params[:feed_image_path].split('/').last @filename ="#{Rails.root}/public/uploads/feed_image/feedimage/#{file_name}" send_file(@filename , :type => 'application/pdf/docx/html/htm/doc', :disposition => 'attachment') end soo simplehttps://stackoverflow.com/questions/22002020/how-to-download-file-with-send-file/..https://stackoverflow.com/questions/22002020/how-to-download-file-with-send-file/..https://stackoverflow.com/questions/22002020/how-to-download-file-with-send-file/.. 

好吧,我建议你把你的文件移动到公共文件夹。 无论如何,这样做

 send_file(Rails.root.join('app' , 'assets', 'images', 'image.jpg')) 

我们需要指定矿井类型以便它将缓存。

 send_file ("#{Rails.root}/public"+image.image_path), :type => "image/jpeg", :disposition => 'inline' 

对于仍在寻找答案的人,send_data和send_file在响应ajax调用时将不起作用。 相反,请尝试提交表单或使用调用控制器方法并下载文件。