无法使用ruby on rails和paperclip gem上传zip文件 [英] Can't upload zip files using ruby on rails and paperclip gem

查看:116
本文介绍了无法使用ruby on rails和paperclip gem上传zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个rails web应用程序。刚刚创建并准备了一些模型和东西。应用程序非常简单。但是现在我有一个问题,就是设置gem paperclip来为模型添加附件。几乎一切都运行正常,如附加图像/ jpg甚至pdf。

I'm working on a rails web application. Just created and prepared some models and stuff. The application is very simple. But now I have a problem by setting up the gem paperclip to add attachments to a model. Almost everything works fine, like attaching images/jpg or even pdf.

但是我无法上传zip文件。我尝试了不同的zip文件,但我总是得到:
Attachment Paperclip :: Errors :: NotIdentifiedByImageMagickError

But I can't upload zip files. I tried different zip files, but I'm always getting: "Attachment Paperclip::Errors::NotIdentifiedByImageMagickError"

这是我的模型:

class Order < ActiveRecord::Base
  has_attached_file :attachment, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :attachment, :content_type => ["application/pdf", "application/zip", "application/x-zip", "application/x-zip-compressed","application/octet-stream","image/jpg","image/png"]
  belongs_to :client
  has_one :status
end

I我正在Mac(Yosemite)上开发,通过brew安装imagemagick并使用SQLite。

I'm developing on a Mac (Yosemite), installed imagemagick via brew and using SQLite.

我将此添加到我的Gemfile中:

I added this to my Gemfile:

gem "paperclip", "~> 4.2"
gem 'cocaine', '~> 0.5.4'

我在过去的几个小时里对谷歌做过研究,还有很多人在回形针上苦苦挣扎,但我没有发现任何人在上传zip文件时遇到问题。

I did research on google the last hours, and there are many people struggling with paperclip, but I didn't find anybody with problems uploading zip files.

也许有人可以在这里提供帮助。
谢谢

Maybe someone can help here. Thanks

推荐答案

ImageMagick无法读取.zip文件。请在此处查看可接受的文件类型:

ImageMagick can't read .zip files. See the acceptable file types here:

http ://www.imagemagick.org/script/formats.php

如果您尝试从zip文件生成缩略图,ImageMagick应该每次都失败时间。

If you're trying to generate a thumbnail from a zip file ImageMagick should fail every time.

尝试将此添加到您的模型中:

Try adding this to your model:

before_post_process :skip_for_zip

def skip_for_zip
   ! %w(application/zip application/x-zip).include?(asset_content_type)
end

然后你的应用程序不会尝试将zip文件作为图像处理

Then your app won't try to process zip files as images

这篇关于无法使用ruby on rails和paperclip gem上传zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆