how to force send_data to download the file in the browser?

余生颓废 提交于 2019-12-04 11:19:48

It's not the browser that's buffering/delaying, it's your Ruby server code.

You're downloading the entire file from S3 before sending it back to the user as an attachment.

It may be better to serve this content to your user directly from S3 using a redirect. Here's a link to building temporary access URLs that will allow a download with a given token for a short period of time:

http://docs.amazonwebservices.com/AmazonS3/latest/dev/S3_QSAuth.html

Base.establish_connection!( :access_key_id => 'my_key', :secret_access_key => 'my_super_secret_key')
s3File = S3Object.find dir+filename, "my_unique_bucket"
redirect_to s3File.url(:expires_in => 30)
Joshua Pinter

Set Your Content Disposition

You'll need to set the content-disposition of the S3 url for it download instead of opening up in the browser. Here is my basic implementation:

Think of attachment as your s3file.

In your attachment.rb

def download_url
  s3 = AWS::S3.new.buckets[ 'bucket_name' ]

  s3.url_for( :read,
    expires_in: 60.minutes, 
    use_ssl: true, 
    response_content_disposition: "attachment; filename='#{file_name}'" ).to_s
end

In your views

<%= link_to 'Download Avicii by Avicii', attachment.download_url %>

Thanks to guilleva for his guidance.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!