【问题标题】:Send_data returning corrupted filesSend_data 返回损坏的文件
【发布时间】:2017-05-22 00:32:14
【问题描述】:

我的资源控制器中有以下下载操作:

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),然后将该文件流式传输到浏览器。 我尝试了多种不同的参数配置到 send_data,我也尝试了 send_file。我最接近可行的解决方案是下载 PDF 文件但它已损坏。 (有时它不小心下载了一个文本文档,而不是预期的文件内容,我得到了文件的内存对象,如下所示。

#<File:0x007fcdda128ea0>

我感觉下载 PDF 时也会发生同样的情况,只是 PDF 查看器显然无法呈现该 PDF。

我确保存储在数据库中的 URL 确实指向正确的文件,并且 data 变量已正确初始化。在将数据发送到浏览器之前,一切都按预期工作。

我这样做而不是重定向到文件 url 的原因是因为我不想将 Google 存储上的文件位置暴露给用户(因此临时下载到服务器)

【问题讨论】:

    标签: ruby-on-rails google-cloud-storage carrierwave sendfile


    【解决方案1】:
    send_data data.read, disposition: 'attachment', stream: 'true', buffer_size: '4096'
    

    添加stream: true

    【讨论】:

    • 你能解释一下吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    • 2020-06-23
    • 1970-01-01
    • 2020-10-15
    • 2021-01-30
    • 2019-03-06
    相关资源
    最近更新 更多