【问题标题】:rails send_file and send_data sends out zero byte filesrails send_file 和 send_data 发送零字节文件
【发布时间】:2011-06-18 01:04:47
【问题描述】:

我正在尝试将 pdf 发送回给用户,但在让 send_file 和 send_data 正常工作时遇到了严重问题。我创建的pdf文件如下:

tmp = Tempfile.new('filled')
new_tmp_path = PDFPrint.fill_form_using_pdftk(template_path, tmp.path)
send_file (new_tmp_path, :filename => 'filled.pdf')

浏览器提示下载,但下载的 Filled.pdf 文件字节为零。 我已验证 new_tmp_path 确实包含有效的 pdf(良好的填充内容)

我试过这个:

File.open(new_tmp_path, 'r') do |f|
  send_data(f.read, :filename => "filled.pdf")
end

但这也给了我同样的下载->零字节问题,而服务器上的文件(new_tmp_path)具有完美的内容。

问候,

【问题讨论】:

    标签: ruby-on-rails pdftk temporary-files sendfile


    【解决方案1】:

    尝试发送一个简单的文件,看看它是否有效

    send_file '/path/to.jpeg', :type => 'image/jpeg', :disposition => 'inline'
    

    阅读this thread,我认为它有你需要的一切。

    【讨论】:

    • 原来我必须在发送之前“刷新”PDFTK 创建的文件。很抱歉没有在问题中提及更多细节。
    猜你喜欢
    • 1970-01-01
    • 2011-04-13
    • 2017-03-01
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 2017-01-01
    相关资源
    最近更新 更多