【问题标题】:How to download a file with Rails 4 Paperclip using s3如何使用 s3 下载带有 Rails 4 Paperclip 的文件
【发布时间】:2015-06-18 21:51:29
【问题描述】:

我有一个使用回形针和 s3 上传文件附件的项目。我怎样才能让用户下载这些文件。

型号

class Assets
  has_attached_file :file
end

路线

resources :assets do
  member do
    get 'download'
  end
end

控制器

  def download
    data = open(@asset.file.url)
    send_data data.read, :type => data.content_type, :x_sendfile => true, :url_based_filename => true
  end

错误:

AssetsController#download 中的 OpenURI::HTTPError

403 禁止

【问题讨论】:

  • 网络上有无数的资源,因此涵盖了这一点。建议你先看看这些,如果你遇到困难(代码,错误等),尝试自己做这个和帖子。
  • 我对此尝试了不同的解决方案,但我不清楚。
  • @steveklein 我提供了有关我正在从事的工作的更多信息

标签: ruby-on-rails-4 amazon-s3 paperclip


【解决方案1】:

我找到了解决这个问题的方法here

答案:

def download
  extension = File.extname(@asset.file_file_name)
  send_data open("#{@asset.file.expiring_url(10000, :original)}").read, filename: "original_#{@asset.id}#{extension}", type: @asset.file_content_type
end

【讨论】:

    猜你喜欢
    • 2011-09-05
    • 2014-05-15
    • 2014-04-14
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多