如何使用send_file下载文件?
-
20-12-2019 - |
题
有人可以告诉我如何下载文件吗 send_file
?
我有一个文件 image.jpg
里面 app/assets/images
. 。我在我的控制器中尝试过:
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 simple......
好吧,我建议您将文件移至公用文件夹。不管怎样,就这样做
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 调用时将不起作用。相反,请尝试提交表单或使用 <a href=..>
调用控制器方法并下载文件。
不隶属于 StackOverflow