【问题标题】:send_file for a tempfile in Sinatrasend_file 用于 Sinatra 中的临时文件
【发布时间】:2013-03-03 07:51:40
【问题描述】:

我正在尝试使用 Sinatra 的内置 send_file 命令,但它似乎不适用于临时文件。

我基本上会执行以下操作来压缩 mp3 专辑:

get '/example' do
  songs = ...
  file_name = "zip_test.zip"
  t = Tempfile.new(['temp_zip', '.zip'])
  # t = File.new("testfile.zip", "w")
  Zip::ZipOutputStream.open(t.path) do |z|
    songs.each do |song|
      name = song.name
      name += ".mp3" unless name.end_with?(".mp3")
      z.put_next_entry(name)
      z.print(open(song.url) {|f| f.read })
      p song.name + ' added to file'
    end
  end
  p t.path
  p t.size

  send_file t.path, :type => 'application/zip',
                         :disposition => 'attachment',
                         :filename => file_name,
                          :stream => false
  t.close
  t.unlink
end

当我使用t = File.new(...) 时,一切正常,但我不想使用File,因为它会出现并发问题。

当我使用t = Tempfile.new(...) 时,我得到:

!! Unexpected error while processing request: The file identified by body.to_path does not exist`

编辑:看起来问题的一部分是我正在发送多个文件。如果我只发送一首歌曲,Tempfile 系统也能正常工作。

【问题讨论】:

  • 如果您使用包含多个文件的预构建 zip 文件而不是使用 Zip::ZipOutputStream.open... 会发生什么?另外,send_file sends a halt 所以不需要t.closet.unlink(无论如何文件系统都应该为你处理这个问题)。

标签: ruby sinatra


【解决方案1】:

我的猜测是您的歌曲名称中有一个拼写错误,或者可能是 song.url 的最后一部分中的一个斜线?我adopted your code 如果所有歌曲都存在,那么将 zip 作为临时文件发送就可以了。

【讨论】:

    猜你喜欢
    • 2017-09-24
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 2012-08-26
    • 2016-01-08
    • 2020-09-03
    相关资源
    最近更新 更多