Rails 'link_to' to Download An Image Immediately Instead of Opening it in the Browser

前端 未结 5 1246
我寻月下人不归
我寻月下人不归 2020-12-31 02:11

I have a link_to Rails helper that downloads a wallpaper when clicked. But the image is loading in the browser instead of being downloaded immediately.

<%         


        
5条回答
  •  长情又很酷
    2020-12-31 02:45

    Generally, the cleanest way to do this is to set the appropriate header when sending the image:

    Content-Disposition: attachment; filename=<file name.ext>
    

    The send_file method will allow you to set this header appropriately if you're serving the file from the filesystem:

    http://api.rubyonrails.org/classes/ActionController/Streaming.html#method-i-send_file

    If the file is stored in your database, you can use send_data instead:

    http://api.rubyonrails.org/classes/ActionController/Streaming.html#method-i-send_data

提交回复
热议问题