【问题标题】:How to set content length by send_file如何通过 send_file 设置内容长度
【发布时间】:2015-06-11 15:55:22
【问题描述】:

我不知道如何通过send_file设置内容长度。

我检查了 api,没有 content-length 参数。

【问题讨论】:

    标签: ruby-on-rails sendfile


    【解决方案1】:

    您可以为响应设置标题,例如:

    def download
      @file = Attachment.find params[:id]
    
      response.headers['Content-Length'] = @file.size.to_s
      send_file @file.path, :x_sendfile => true
    end
    

    您可以在official docs 上找到有关响应对象的更多信息。

    P.S: Header 需要是一个字符串才能与某些网络服务器正常工作。

    【讨论】:

    • 谢谢!但我收到错误 502: Bad Gateway / nginx/1.4.1 ,我现在无法上传图片,抱歉
    • 您是否正在从您的应用程序上传或下载图像?
    • 下载生成的zip,我用的是nginx,是rails还是nginx配置错误?
    • 您可以查看您的应用程序日志,但我认为 502 来自 nginx。还要检查 nginx 的 error.logaccess.log 以获取更多信息。
    • 看起来像是这篇文章的复制/粘贴。 stackoverflow.com/questions/22726345/…
    猜你喜欢
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 2015-06-15
    • 1970-01-01
    • 2011-10-27
    • 2015-01-06
    • 1970-01-01
    相关资源
    最近更新 更多