【问题标题】:send PDF with rails send_data使用 rails send_data 发送 PDF
【发布时间】:2014-04-15 12:38:14
【问题描述】:

我使用 ruby​​ 1.9.3 和 redmine 1.4.4

据此 -> Please help me send a jpg file using send_data ,我在控制器中执行此操作:

  @file = temp.path
  File.open(@file, 'r') do |f|
    send_data f.read, :filename => "myfile.pdf", :type => "application/pdf", :disposition => "attachment"
  end
  File.delete(@file)

但它返回ArgumentError (invalid byte sequence in UTF-8),为什么?

【问题讨论】:

  • send_file 不起作用?
  • send_file 有效,但我想最后做File.delete(@file)。我编辑我的问题。
  • 为什么不能用send_file发送文件然后删除?
  • 因为它给我发送了损坏的 pdf...stackoverflow.com/questions/22933871/…
  • 使用send_file时,不需要File.open do |f|等,只需send_file @file, :disposition => "attachment"即可。你试过了吗?

标签: ruby-on-rails ruby redmine


【解决方案1】:

PDF 文件必须编码

 file = temp.path
 File.open(file, 'r') do |f|
   send_data f.read.force_encoding('BINARY'), :filename => filename, :type => "application/pdf", :disposition => "attachment"
 end
 File.delete(file)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 2021-01-26
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    相关资源
    最近更新 更多