【问题标题】:How create zip file using rubyzip on heroku如何在heroku上使用rubyzip创建zip文件
【发布时间】:2013-09-17 14:54:42
【问题描述】:

我正在阅读用户简历后创建 zip 文件。代码在本地机器上运行良好。但是在heroku上部署此代码时出现错误。

   def get_stream(contractors)
    #t = Tempfile.new("my-temp-filename-#{Time.now}")  #this code on local
    t = Tempfile.new("#{Rails.root}/tmp/my-temp-filename-#{Time.now}") #this line on heroku break
    begin
      Zip::OutputStream.open(t.path) do |zos|
        contractors.each_with_index do |contractor, index|
          if contractor.cv.path
            zos.put_next_entry("#{index+1}_#{contractor.full_name.parameterize.underscore}.#{contractor.cv_file_name.split(".").last}")
            zos.print IO.read(contractor.cv.path)
         end
     end
   end
  send_file t.path, :type => 'application/zip', :filename => "Contractors_cvs.zip", :x_sendfile => true
    t.close
  rescue
    t.close
  end

结束

heroku 记录 --app 时只显示“在 56 毫秒内完成 500 个内部服务器错误”

请帮忙。

谢谢

【问题讨论】:

    标签: ruby-on-rails-3 rubyzip


    【解决方案1】:

    最后这对我在本地和 heroku 上都有效。

         def get_stream(contractors)
           begin
            Zip::File.open("#{Rails.root}/tmp/zipfile_name.zip", Zip::File::CREATE) do |zipfile|
            contractors.each_with_index do |filename, index|
          zipfile.add(filename.try(:cv_file_name), filename.try(:cv).try(:path)) if filename.present?
                end
               end
    
          send_file "#{Rails.root}/tmp/zipfile_name.zip", :type => 'application/zip', :filename => "Contractor_cvs.zip", :x_sendfile => true
      File.delete("#{Rails.root}/tmp/zipfile_name.zip")
    
           rescue         
           end
         end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多