发送文件到AWS S3使用CarrierWave

3

我希望下载上传到aws s3的文件:

控制器

def download
    send_file my_file.url
end

实际上我尝试了所有在类似帖子中找到的代码:

send_file open(my_file.url).read

同时没有阅读。没有任何作用。


你最终解决了这个问题吗? - Serge Pedroza
没有,我还没能够弄清楚 :) - clement
3个回答

3
另一种选择是直接从您的carrierwave对象中进行读取,并使用send_data而不是send_file。如果您的文件也存在于其本地缓存中,那么您将得到carrierwave使用自己的本地缓存的额外好处。
无论如何,您可以尝试按以下方式使用send_data:
send_data(my_file.file.read, filename: 'your_txt_file.txt', type: 'text/plain', disposition: 'attachment', stream: 'true', buffer_size: '4096')

如果您想让用户直接下载文件,您应该指定disposition: 'attachment'。如果您希望文件在用户自己的浏览器内呈现,则可以使用disposition: 'inline'


1

控制器

def download
  redirect_to my_file.file.url
end

0

所以这是我最终用于pfd和zip文件的解决方案。还没有找出如何处理images/png或jpeg文件。我的模型名称是Import。希望这能有所帮助。

视图

<%= link_to 'Download', import.image.url %>

控制器

def download_file
  @import = Import.find(params[:id])
  send_file(@import.image.path,
      :disposition => 'attachment',
      :url_based_filename => false)
end

路由

resources :imports do
  collection do
    get 'download'      
  end
end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接