Ruby on rails 发送数据返回损坏的文件
我的资源控制器中有以下下载操作:Ruby on rails 发送数据返回损坏的文件,ruby-on-rails,google-cloud-storage,carrierwave,sendfile,Ruby On Rails,Google Cloud Storage,Carrierwave,Sendfile,我的资源控制器中有以下下载操作: def download require 'open-uri' if validate_token(safe_params[:x]) resource = Resource.find_by_token(safe_params[:x]) data = open(resource.file.url) send_data( data, :disposition => 'attachment',:
def download
require 'open-uri'
if validate_token(safe_params[:x])
resource = Resource.find_by_token(safe_params[:x])
data = open(resource.file.url)
send_data( data, :disposition => 'attachment',:url_based_filename => true, type: data.meta['content-type'].to_s)
# send_file data, disposition: 'attachment'
# redirect_to resource.file.url
puts data.hash
puts data.meta['x-goog-hash']
else
redirect_to pages_error_path, notice: 'Does not match resource.'
end
end
该操作下载存储在Google Storage上的文件(使用carrierwave
),然后将该文件流式传输到浏览器。
我尝试了多种不同的参数配置来发送_数据,也尝试了发送_文件。我找到的最接近工作解决方案是下载PDF文件,但它已损坏。(有时它会意外地下载一个文本文档,我得到的不是预期的文件内容,而是文件的内存对象,如下所示
#<File:0x007fcdda128ea0>
#
我感觉PDF下载也会发生同样的情况,只是PDF查看器显然无法呈现该PDF
我确保存储在数据库中的URL指向正确的文件,并且数据
变量已正确初始化。在数据发送到浏览器之前,一切正常
我之所以这样做而不是重定向到文件url,是因为我不想向用户公开Google存储上的文件位置(因此临时下载到服务器)
加载项
stream:true
您能提供一些解释吗?
send_data data.read, disposition: 'attachment', stream: 'true', buffer_size: '4096'