使用paperclip上传静态zipfile失败,文件名为Paperclip :: AdapterRegistry :: NoHandlerError .zip

时间:2014-03-18 16:36:35

标签: ruby-on-rails amazon-s3 paperclip zipfile

我在临时文件夹中创建了zip文件,但是我无法将静态zip文件上传到s3。它失败并出现错误Paperclip :: AdapterRegistry :: NoHandlerError for myfilename.zip.Here是我的代码

控制器

def create_zipfile
     file = File.join(Rails.root, "public/temp_download/invoices_#{timer}.zip")
     Zip::Archive.open(file, Zip::CREATE) do |ar|
     Dir.glob("#{Rails.root}/public/temp_download/invoices_#{timer}/*").each do |path|
     ar.add_file(path.split('/').last, path) # add_file(<entry name>, <source path>)
     end
  end
  upload(pfile)
end

def upload(filename)
     invoice_upload = InvoiceUpload.new
     invoice_upload.invoice =  filename
     invoice_upload.save
end

模型

class InvoiceUpload < ActiveRecord::Base
                      has_attached_file :invoice,
                      :storage => :s3,
                      :url     => 's3_domain_url',
                      :s3_host_alias  => '***'
                      :s3_credentials => File.join(Rails.root, 'config', 's3.yml'), 
                      :path => "/invoices/:style/:id_:filename"
end

1 个答案:

答案 0 :(得分:2)

更改上传方法,如下所示:

def upload(filename)
     invoice_upload = InvoiceUpload.new
     invoice_upload.invoice =  Rack::Test::UploadedFile.new(filename)
     invoice_upload.save
end

此外,upload(pfile) upload(file)应为create_zipfile,因为代码中没有名为pfile的变量。