我正在使用Linode作为我的托管解决方案。我有一个rails 3应用程序,可动态获取mp3(和其他媒体)并创建一个.zip文件供下载。它在开发中工作正常但是一旦我把它放在我的prod服务器上,zip文件仍然下载但是当我解压缩它时,它会创建一个名为foo-bar.zip.cpgz的文件
来自我的控制器的代码片段 -
def get_zip
t = Tempfile.new("#{@foobar.slug}-#{request.remote_ip}.zip")
Zip::ZipOutputStream.open(t.path) do |zos|
@foobardownloads.each do |foobardownload|
extension = File.extname(foobardownload.foobardownload_file_name).gsub(/^\.+/, '')
zos.put_next_entry("#{foobardownload.title}.#{extension}")
zos.print open(foobardownload.foobardownload.url).read
end
end
send_file t.path, :x_sendfile => true, :type => 'application/zip', :filename => "#{@foobar.slug}.zip"
t.close
end
答案 0 :(得分:6)
http://www.novafist.com/2010/09/send_file-sends-0-bytes-to-client-in-rails/
“快速而肮脏”的黑客攻击是打开你的production.rb文件并取消注释这一行
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
确保
#config.action_dispatch.x_sendfile_header = "X-Sendfile"
仍然被注释掉了。