Send_data 返回损坏的文件
Send_data returning corrupted files
我的资源控制器中有以下下载操作:
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 存储中的文件(使用 carrierwave
),然后将该文件流式传输到浏览器。
我尝试了 send_data 的多种不同参数配置,我也尝试了 send_file。我最接近可行的解决方案是下载 PDF 文件但它已损坏。 (有时它不小心下载了一个文本文档,而不是预期的文件内容,我得到了文件的内存对象,如下所示。
#<File:0x007fcdda128ea0>
我感觉 PDF 下载时也会发生同样的情况,只是 PDF 查看器显然无法呈现该 PDF。
我确保存储在数据库中的 URL 确实指向正确的文件并且 data
变量被正确初始化。在将数据发送到浏览器之前,一切都按预期工作。
我这样做而不是重定向到文件 url 的原因是因为我不想向用户公开 Google 存储上的文件位置(因此临时下载到服务器)
send_data data.read, disposition: 'attachment', stream: 'true', buffer_size: '4096'
加入stream: true
我的资源控制器中有以下下载操作:
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 存储中的文件(使用 carrierwave
),然后将该文件流式传输到浏览器。
我尝试了 send_data 的多种不同参数配置,我也尝试了 send_file。我最接近可行的解决方案是下载 PDF 文件但它已损坏。 (有时它不小心下载了一个文本文档,而不是预期的文件内容,我得到了文件的内存对象,如下所示。
#<File:0x007fcdda128ea0>
我感觉 PDF 下载时也会发生同样的情况,只是 PDF 查看器显然无法呈现该 PDF。
我确保存储在数据库中的 URL 确实指向正确的文件并且 data
变量被正确初始化。在将数据发送到浏览器之前,一切都按预期工作。
我这样做而不是重定向到文件 url 的原因是因为我不想向用户公开 Google 存储上的文件位置(因此临时下载到服务器)
send_data data.read, disposition: 'attachment', stream: 'true', buffer_size: '4096'
加入stream: true