Paperclip: Cannot attach XLS (But DOC works)

元气小坏坏 提交于 2019-12-11 05:57:40

问题


I cannot attach a XLS file, but DOC works:

Attachments file content type is invalid
Attachments file is invalid

Here is the log:

Parameters: ... "files"=>[#<ActionDispatch::Http::UploadedFile:0x0000000daf7730 @tempfile=#<Tempfile:C:/Users/Chloe/AppData/Local/Temp/RackMultipart20170511-47156-ym774u.xls>, @original_filename="Chocolate_Store1.xls", @content_type="application/vnd.ms-excel", @headers="Content-Disposition: form-data; name=\"deal[files][]\"; filename=\"Chocolate_Store1.xls\"\r\nContent-Type: application/vnd.ms-excel\r\n">],
...
Command :: file -b --mime "C:/Users/Chloe/AppData/Local/Temp/1f1fb4d71efc0bd245abd7b5b9aa661220170511-47156-1rqzd0q.xls"
   (1.0ms)  ROLLBACK
  Rendering deals/edit.html.erb within layouts/application

When I test the type:

>file -b --mime "C:/Users/Chloe/AppData/Local/Temp/1f1fb4d71efc0bd245abd7b5b9aa661220170511-47156-1rqzd0q.xls"
application/vnd.ms-office; charset=binary

I have this type in my accept list:

class Attachment < ApplicationRecord
  has_attached_file :file
  validates_attachment_content_type :file, content_type: [
    "application/pdf", "application/msword", 
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 
    "application/vnd.oasis.opendocument.text",
    "application/vnd.ms-excel",
    "application/vnd.ms-office",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    "application/vnd.oasis.opendocument.spreadsheet"
   ]

end

Rails 5.0.2, Paperclip 5.1.0, File 5.04 (DevKit)


I noticed DevKit file differs from Cygwin file. Not sure if relevant:

>file -b --mime "C:/Users/Chloe/AppData/Local/Temp/1f1fb4d71efc0bd245abd7b5b9aa661220170515-54920-1e2bk5s.xls"
application/vnd.ms-office; charset=binary

$ file -b --mime "C:/Users/Chloe/AppData/Local/Temp/1f1fb4d71efc0bd245abd7b5b9aa661220170515-54920-1e2bk5s.xls"
application/vnd.ms-excel; charset=binary

回答1:


I switched to use

validates_attachment_file_name :file, matches: [/\.pdf$/, /\.docx?$/, /\.xlsx?$/, /\.odt$/, /\.ods$/]



回答2:


...content_type: [...'application/x-ole-storage'...]

worked for me



来源:https://stackoverflow.com/questions/43927006/paperclip-cannot-attach-xls-but-doc-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!