Rails下载文件的三种方法
Solution 1
Generally, the cleanest way to do this is to set the appropriate header when sending the image:
Content-Disposition: attachment; filename=<file name.ext>
class ApplicationController < ActionController::Base before_filter :set_headers private def set_headers @headers["Content-Type"] = "text/csv"
@headers["Content-disposition"] = 'attachment; filename="the_file.csv"' end end
自定义HTTP响应页头,HTTP response header
def add_headers response.headers['test-controller-header'] = 'test-controller-header-value' end
The send_file method will allow you to set this header appropriately if you're serving the file from the filesystem:
If the file is stored in your database, you can use send_data instead:
Solution 2
<%= link_to 'Download existing avatar', @user.avatar(:original), download: "User_#{@user.id}_avatar" %>
Solution 3
Instead of putting the link of the image in your tag, you can handle it in your controller. And then in your controller you can do something like
send_file @download.wallpapers[1].wallpaper.url, :type => 'image/jpeg', :disposition => 'attachment'
阅读量: 1640
发布于:
修改于:
发布于:
修改于: