ruby-on-rails - 办公文件(docx、pptx)的回形针上传正在下载为 zip 文件?

标签 ruby-on-rails ruby-on-rails-3 amazon-s3 paperclip mime-types

我使用以下文件上传:Rails 3.2、Paperclip (3.0.4)、aws-sdk (1.5.2) & jQuery-File-Upload

问题是像 (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", Wed, 30 May 2012 00:54:09 UTC +00:00], ["deleted", false], ["room_id", 20], ["pinned", false], ["updated_at", Wed, 30 May 2012 00:54:09 UTC +00:00], ["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 文件。任何使用普通 mimetypes 的东西都会假设它是一个压缩文件。

要解决此问题,您必须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!()方法,在 this Stackoverflow answer 中可见,如果您必须支持所有 Office 2007 文件。

关于ruby-on-rails - 办公文件(docx、pptx)的回形针上传正在下载为 zip 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10808734/

相关文章:

ruby-on-rails - 为什么无法正确接收参数?

ruby-on-rails - 混淆实例变量和局部变量

ruby-on-rails - Rails3 Routes - 将参数传递给成员路由

java - 将文件存储到 S3 : local file vs HDFS

ruby-on-rails - 在公共(public)事件中担任投票人

ruby-on-rails - 没有要加载的文件 -- nokogiri

ruby-on-rails - UnInitialized Contant Twitter::Grape 使用 grape 示例代码

ruby-on-rails - 如何在 new.js.erb 中使用局部变量 f 渲染 RAILS 部分?

python - HTTP代码400在python中使用Boto3从AWS S3存储桶获取对象

amazon-web-services - Amazon S3 验证预签名 URL