excel - xls 和 xlsx 的回形针内容类型

标签 excel ruby-on-rails-4 paperclip paperclip-validation

遇到回形针内容类型问题,需要上传 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."]}

最佳答案

错过了路径; (

通过使用修复它

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.'

关于excel - xls 和 xlsx 的回形针内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36261777/

相关文章:

excel - VBA:用上面的值加一填充空白单元格

VB.NET - 插入 Excel 电子表格时出现 OleDbException 说电子表格已满

ruby-on-rails - 可以通过某些词来订购吗?

ruby-on-rails - 如何显示 Time.now 基于 TimeZone - Rails 4

ruby-on-rails - 从 3.5 升级到 Paperclip 4.1 时没有 validates_attachment_file_name

ruby-on-rails - Rails 4.2,回形针 gem 。无法附加 .docx 类型,尽管已注册 MIME 类型,但读取 content_type 为 'application/zip'

ruby-on-rails - 回形针:从带有扩展名的 url 上传

c# - 在 OpenXML (excel) 中创建自定义列宽

excel - 计算过滤列中有多少个不同值(或获取不同值列表)

ruby-on-rails - rails 4 : form_for with associations