如何强制SEND_DATA下载在浏览器中的文件? [英] how to force send_data to download the file in the browser?

查看:200
本文介绍了如何强制SEND_DATA下载在浏览器中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么我的问题是,我使用 SEND_DATA 我的轨道3 应用程序发送给用户从 AWS S3 服务文件的东西,如

Well my problem is that I'm using send_data on my Rails 3 application to send to the user a file from AWS S3 service with something like

Base.establish_connection!( :access_key_id => 'my_key', :secret_access_key => 'my_super_secret_key')
s3File = S3Object.find dir+filename, "my_unique_bucket"
send_data(open(s3File.url).read,:filename=>filename, :disposition => 'attachment')

但好像浏览器缓存文件和缓冲之前将其发送到下载服用没有时间上的下载文件,但在缓存时间它采取只要文件大小....但我需要的是用户查看下载过程正常,就不会知道与装载机仅在浏览器选项卡中发生的事情:

but seems like the browser is buffering the file and before buffering it sends the file to download taking no time on the download but at the buffering time it's taking as long as the file size .... but what i need is the user to view the download process as normal, they won't know what happening with the loader only on the browsers tab:

他们宁愿看到一个下载过程中,我想弄清楚有什么东西发生有

They'd rather see a download process i guess to figure out there's something happening there

有没有什么办法,我可以用 SEND_DATA

is there any way i can do this with send_data?

推荐答案

这不是浏览器的缓存/延迟,这是你的Ruby服务器code。

您正在下载从S3整个文件发送回用户作为附件之前。

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

这可能是最好直接从S3使用重定向服务于这个内容,你的用户。这里有一个链接,以建立临时访问的URL,将允许下载与给定令牌很短的时间周期:

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:

<一个href="http://docs.amazonwebservices.com/AmazonS3/latest/dev/S3_QSAuth.html">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)

这篇关于如何强制SEND_DATA下载在浏览器中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆