- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我已经注册了一个 MIME 类型以允许 Paperclip 读取 .docx
文件的 content_type 作为 application/vnd.openxmlformats-officedocument.wordprocessingml.document
。
但是在测试中,content_type 仍被读取为 application/zip
。
知道为什么吗?更令人沮丧的是,.pptx
和 .xlsx
mime 类型已经被注册,这些测试通过了(呃)。
config/initializers/mime_types.rb
Mime::Type.register 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', :docx
Mime::Type.register 'application/vnd.openxmlformats-officedocument.presentationml.presentation', :pptx
Mime::Type.register 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', :xlsx
app/models/attachment.rb
class Attachment < ActiveRecord::Base
include FormatFile
# Constants:
VALID_FILE_NAMES = [/[^a-z0-9\-]+/i].freeze
VALID_IMAGE_TYPES = %w[image/jpeg image/jpg image/png image/x-icon image/bnp].freeze
VALID_CONTENT_TYPES = %w[
image/jpeg
image/jpg
image/gif
image/png
image/bmp
image/x-icon
text/plain
text/csv
application/xml
application/pdf
application/msword
application/vnd.openxmlformats-officedocument.wordprocessingml.document
application/vnd.openxmlformats-officedocument.wordprocessingml.template
application/vnd.ms-word.document.macroEnabled.12
application/vnd.ms-word.template.macroEnabled.12
application/vnd.ms-excel
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
application/vnd.openxmlformats-officedocument.spreadsheetml.template
application/vnd.ms-excel.sheet.macroEnabled.12
application/vnd.ms-excel.template.macroEnabled.12
application/vnd.ms-excel.addin.macroEnabled.12
application/vnd.ms-excel.sheet.binary.macroEnabled.12
application/vnd.ms-powerpoint
application/vnd.openxmlformats-officedocument.presentationml.presentation
application/vnd.openxmlformats-officedocument.presentationml.template
application/vnd.openxmlformats-officedocument.presentationml.slideshow
application/vnd.ms-powerpoint.addin.macroEnabled.12
application/vnd.ms-powerpoint.presentation.macroEnabled.12
application/vnd.ms-powerpoint.template.macroEnabled.12
application/vnd.ms-powerpoint.slideshow.macroEnabled.12
].freeze
# Associations:
belongs_to :attachable, polymorphic: true
# Paperclip attachments
has_attached_file :attachment,
styles: {
medium: ['300x300#', :png],
thumb: ['100x100#', :png],
original: ['500x500>', :png]
},
default_url: '/images/:style/missing.png',
url: '/system/:class/:attachment/:id_partition/:style/:hash.:extension',
path: ':rails_root/public:url',
hash_secret: '623629947a471569fe9808ab386f6e866abde5f582485beaa24fa12032b28a21b6ee94c018fe531484bb438a7376d4a00b4bc35598de34c01f0e40b1dbb37df5'
# Validations:
validates_attachment :attachment,
content_type: {content_type: VALID_CONTENT_TYPES},
file_name: {matches: VALID_FILE_NAMES},
size: {in: 0..5.megabytes}
validates_with AttachmentPresenceValidator, attributes: :attachment
# Callbacks:
before_post_process :skip_all_non_images
before_validation do
sanitize_filename attachment_file_name
end
# Don't shrink and create different styles for anything which isn't an image
def skip_all_non_images
VALID_IMAGE_TYPES.include?(attachment_content_type)
end
end
感谢您的帮助。
编辑:
根据 bkunzi01 提供的链接,我尝试了以下代码。它没有效果,.docx
文件仍然无法通过验证,而 .pptx
和 .xlsx
通过。
config/application.rb
Paperclip.options[:content_type_mappings] = {
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
}
最佳答案
看来是新版本的回形针导致了这个问题。降级到版本 4.3.1 应该可以解决您的问题
关于ruby-on-rails - Rails 4.2,回形针 gem 。无法附加 .docx 类型,尽管已注册 MIME 类型,但读取 content_type 为 'application/zip',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45348662/
这里是 Ruby 初学者!我正在尝试在 ruby 上创建一个 Soundcloud 克隆。当我尝试上传音频文件时出现错误: 1 个错误禁止保存这首歌: 音频中的内容与所报告的内容不同 Contro
这是我配置附件的方式: has_attached_file :image, {styles: { small: "64x64", med: "100x100", large: "200x200" },
最近我正在调整我的 rails 应用程序以在 JRuby 上运行。 我遇到的问题之一是 Paperclip。 Paperclip 使用 Cocaine 来运行命令行工具,如 ImageMagick,它
我是 RoR 回形针 gem 的新手,我一直在尝试有关多张图像上传的一些示例,并希望了解有关这个伟大 gem 的一些信息。 在一个例子中,我有一个相册模型,它允许用户上传多张图片。我注意到图像存储在
有没有人有多个附件的 Rails 3 示例,可以在多部分表单上进行验证?我一直试图让它永远工作(并且找到了我能找到的所有博客文章和消息,但没有一个涵盖这种情况,而且文档根本没有帮助)。 第一个问题
当用户在我的应用程序中编辑某些内容时,即使他们没有更改图像,他们也被迫通过回形针重新上传图像。不这样做会导致错误,因为我validate_presence_of :image。这很烦人。 如果用户根本
我正在使用回形针在我的 Rails 应用程序中上传和显示图像。 '') %> 上面的代码显示图像的缩略图。 我想让缩略图成为一个链接,以便当用户点击它时,会显示一个显示原始图像的弹出窗口。 我尝
我希望用户能够保存最多 5 个文件(任何少于这个都很好),所以这是我的新操作(place_photos 是回形针附件,Place :has_many PlacePhoto): def new @p
我正在一个需要接受文件上传的项目。文件上传后,我要进行一些处理-从文件中提取信息。我最终计划在后台工作人员中运行此程序,但当前正在内联运行。 我尝试过同时使用after_create和after_sa
我正在使用回形针来处理我的文件上传,并且在一种情况下我不希望该文件是强制性的。但是,我确实想确保它存在时是特定的文件类型。 我有这个: class TestModel ['audio/mp3', '
我正在使用 Paperclip 4.1.1用于图片上传。我存储三种尺寸,原始尺寸、中等尺寸和缩略图尺寸。当我尝试显示原始图像时,其中一些图像向左旋转,并且随机发生,因为其他一些图像显示正确。中等大小和
我已经成功实现了 jcrop 和回形针,通过转到另一个页面(即 crop.html.erb)来裁剪图像。但我希望能够在当前页面上进行所有裁剪,在弹出的 div/对话框中上传图像。因此,我需要以某种方式
如果我将两个具有相似文件名的文件保存到同一目录中,一个文件将覆盖另一个文件。如何使下面的方式工作,比方说,目录中已经有 picture.jpg,如果我将另一个 picture.jpg 上传到同一目录,
我发现这个教程 (http://www.coffeepowered.net/2009/02/15/graceful-degredation-using-gravatar-as-a-fallback-a
我有一个主题模型,其中包含许多 Assets 。 Assets 使用 Paperclip 并将其文件内容存储在我的 Amazon AWS-S3 系统中。我也在使用 deep_clone,因为我的客户能
当我尝试在回形针-ffmpeg 中上传 .mov 视频时,会出现错误 Video content type is invalid in paperclip-ffmpeg 我的代码是 has_attac
所以,我正在使用 Paperclip 和 AWS-S3,这很棒。而且效果很好。不过,只有一个问题:我需要上传非常大的文件。如超过 50 兆字节。就这样,nginx 死了。那么显然Paperclip在进
我正在尝试查找有关如何设置Paperclip以使用fog.io和fog.io以使用Rackspace Cloud File的文档,但是我找不到任何好的引用文献(而且我认为自己是一位出色的Google语
当我尝试在 Ruby on Rails 上传照片时使用 Paperclip在我的本地机器上,它运行良好。 当我尝试在带有 Apache 和 CentOS 的 Linux(Phusion Passeng
除了动态文件大小限制之外,还有 validates_attachment_size 吗?这是一个例子: class Document get_current_file_size_limit p
我是一名优秀的程序员,十分优秀!