ruby-on-rails – 办公文件(docx,pptx)的PaperClip上传作为zip文件下载?

我正在使用以下文件上传:Rails 3.2,Paperclip(3.0.4),aws-sdk(1.5.2)& jQuery的文件上传

问题是办公室文件(pptx)正在下载为zip文件而不是pptx文件.这是我在日志中看到的:

Started POST
Processing by AttachmentsController#create as JS
  Parameters: {"files"=>[#<ActionDispatch::Http::UploadedFile:0x007fa1d5bee960 @original_filename="test1.pptx",@content_type="application/vnd.openxmlformats-officedocument.presentationml.presentation",@headers="Content-Disposition: form-data; name=\"files[]\"; filename=\"test1.pptx\"\r\nContent-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation\r\n",@tempfile=#<File:/var/folders/rm/89l_3yt93g31p22738hqydmr0000gn/T/RackMultipart20120529-10443-1ljhigq>>]}
.....


sql (1.4ms)  INSERT INTO "attachments" ("attachment_content_type","attachment_file_name","attachment_file_size","attachment_file_title","attachment_updated_at","created_at","deleted","room_id","pinned","updated_at","user_id") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11) RETURNING "id"  [["attachment_content_type","application/zip"],["attachment_file_name","test1_1338339249.pptx"],["attachment_file_size",150329],["attachment_file_title","test1.pptx"],["attachment_updated_at",Wed,30 May 2012 00:54:09 UTC +00:00],["created_at",["deleted",false],["room_id",20],["pinned",["updated_at",["user_id",1]]
[paperclip] Saving attachments.
[paperclip] saving /development/private/rooms/20/user_uploaded_files/test1_1338339249.pptx
Command :: file -b --mime '/var/folders/rm/89l_3yt93g31p22738hqydmr0000gn/T/RackMultipart20120529-10443-1ljhigq20120529-10443-1lr2yg2'
[AWS S3 200 1.16513 0 retries] put_object(:acl=>:private,:bucket_name=>"cdn-assets-site-com",:content_type=>"application/zip",:data=>#<Paperclip::FileAdapter:0x007fa1d2540170 @target=#<File:/var/folders/rm/89l_3yt93g31p22738hqydmr0000gn/T/RackMultipart20120529-10443-1ljhigq>,@tempfile=#<File:/var/folders/rm/89l_3yt93g31p22738hqydmr0000gn/T/RackMultipart20120529-10443-1ljhigq20120529-10443-1lr2yg2>>,:key=>"development/private/rooms/20/user_uploaded_files/test1_1338339249.pptx")

注意文件如何以pptx的形式出现,但是当上传到AWS S3时,可以将其作为zip文件

解决方法

事实证明,正如Marc B首先暗示的那样 – x结尾的所有Office文档确实是压缩的XML文件.任何使用正常模式的东西都会认为它是一个压缩文件.

解决这个问题,你必须要register the Office mimetypes with your server.所以,对于你的.pptx文件,你放了

Mime::Type.register "application/vnd.openxmlformats-officedocument.presentationml.presentation",:pptx

在你的config / initializers / mime_types.rb文件中.

或者,您可以使用Rack :: Mime :: MIME_TYPES.merge!()方法,如果您必须支持所有Office 2007文件,则在this Stackoverflow answer中可以看到该方法.

相关文章

以下代码导致我的问题: class Foo def initialize(n=0) @n = n end attr_accessor :n d...
这是我的spec文件,当为上下文添加测试“而不是可单独更新用户余额”时,我得到以下错误. require 's...
我有一个拦截器:DevelopmentMailInterceptor和一个启动拦截器的inititializer setup_mail.rb. 但我想将...
例如,如果我有YAML文件 en: questions: new: 'New Question' other: recent: ...
我听说在RSpec中避免它,let,let !,指定,之前和主题是最佳做法. 关于让,让!之前,如果不使用这些,我该如...
我在Rails中使用MongoDB和mongo_mapper gem,项目足够大.有什么办法可以将数据从Mongoid迁移到 Postgres...