2016-03-28 106 views
1

挣扎着回形针的内容类型,需要上传xls/xlsx文件。回形针xls和xlsx的内容类型

has_attached_file :sheet 
validates_attachment_content_type :sheet, content_type: [ 
              'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 
              'application/zip', 
              'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 
              'application/vnd.ms-excel', 
              'application/xls', 
              'application/xlsx', 
              'application/octet-stream' 
             ], 
           message: ' Only EXCEL files are allowed.' 

注:创建从谷歌驱动器的片材。

以上内容类型我试过,但每次都得到了同样的错误

输出

Command :: file -b --mime '/var/folders/zy/khy_wsfn7jbd40bsdps7qwqc0000gt/T/5a76e813d6a0a40548b91acc11557bd220160328-13642-1meqjap.xlsx' 
    (0.2ms) BEGIN 
Command :: file -b --mime '/var/folders/zy/khy_wsfn7jbd40bsdps7qwqc0000gt/T/5a76e813d6a0a40548b91acc11557bd220160328-13642-114d8t6.xlsx' 
    (0.3ms) ROLLBACK 
{:sheet_content_type=>[" Only EXCEL files are allowed."], :sheet=>[" Only EXCEL files are allowed."]} 

回答

0

错过了路径; (

使用

has_attached_file :sheet, 
       :path => ":rails_root/public/system/:attachment/:id/:filename" 

validates_attachment :sheet, presence: true, 
        content_type: { content_type: [ 
        "application/vnd.ms-excel", 
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" 
        ] 
        }, 
        message: ' Only EXCEL files are allowed.' 
+0

这是什么有什么关系?它适用于DOC没有路径。为什么不XLS路径修正呢?见http://stackoverflow.com/questions/43927006/ paperclip-can-attach-xls-but-doc-works – Chloe

+0

':path'甚至不是一个选项!http://www.rubydoc.info/github/thoughtbot/paperclip/Paperclip/ClassMethods:has_attached_file – Chloe