首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法上传xls文件,但可以通过Shrine上传xlsx文件

无法上传xls文件,但可以通过Shrine上传xlsx文件
EN

Stack Overflow用户
提问于 2019-09-24 21:37:02
回答 3查看 447关注 0票数 0

我想要上传一个xls文件(Libreoffice)通过Shrine gem,但是我得到一个回滚错误,如文件类型必须为1..(在我的initializers/shorine.rb中有mime类型)..这是我的shrine.rb

代码语言:javascript
复制
 require "shrine"
require "shrine/storage/s3"

s3_options = {
  bucket:            "#{Rails.application.secrets[:aws_bucket_name]}",
  access_key_id:     "#{Rails.application.secrets[:aws_access_key_id]}",
  secret_access_key: "#{Rails.application.secrets[:aws_secret_access_key]}",
  region:            "#{Rails.application.secrets[:aws_region_name]}"
}

Shrine.storages = {
  cache: Shrine::Storage::S3.new(prefix: "cache", **s3_options),
  store: Shrine::Storage::S3.new(prefix: "store", **s3_options),
  #public_store: Shrine::Storage::S3.new(public: true, upload_options: { cache_control: "max-age=15552000" }, **s3_options)
}

Shrine.plugin :activerecord
Shrine.plugin :instrumentation
Shrine.plugin :determine_mime_type, analyzer: :marcel
Shrine.plugin :cached_attachment_data
Shrine.plugin :restore_cached_data

Shrine.plugin :presign_endpoint, presign_options: -> (request) {
  # Uppy will send the "filename" and "type" query parameters
  filename = request.params["filename"]
  type     = request.params["type"]

  {
    content_disposition:    ContentDisposition.inline(filename), # set download filename
    content_type:           type,                                # set content type (required if using DigitalOcean Spaces)
    content_length_range:   0..(10*1024*1024),                   # limit upload size to 10 MB
  }
}

Shrine.plugin :derivation_endpoint,
  secret_key: "secret",
  download_errors: [defined?(Shrine::Storage::S3) ? Aws::S3::Errors::NotFound : Errno::ENOENT]

这是我的shrine_uploader.rb

代码语言:javascript
复制
class ShrineUploader < Shrine

  plugin :processing
  plugin :validation_helpers # to validate image data
  plugin :versions
  plugin :add_metadata
  plugin :delete_raw
  plugin :recache
  plugin :default_storage, cache: :cache, store: :store

  Attacher.validate do
    validate_max_size 10.megabyte
    validate_mime_type_inclusion ['image/jpg', 'image/jpeg', 'image/png', 'image/gif','image/tiff', 'application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/vnd-xls']
  end

  def generate_location(io, context)
    #type  = context[:record].class.name.downcase if context[:record]
    type  = context[:record].patron_id if context[:record]
    style = context[:version] if context[:version]
    name  = super # the default unique identifier[type, style, name].compact.join("/")
  end
end

我可以上传xlsx文件,但是我无法理解为什么即使mime类型是application/vnd.ms-excel也不能上传xls文件。

谢谢你的帮助。

EN

回答 3

Stack Overflow用户

发布于 2020-09-30 08:03:55

代码语言:javascript
复制
class FileUploader < Shrine
  plugin :validation_helpers
  plugin :pretty_location
  plugin :determine_mime_type

  Attacher.validate do
    validate_max_size 150.megabytes, message: "Large file sorry"
    #validate_mime_type %w[image/jpeg image/png image/jpg]
    validate_extension_inclusion %w[jpg jpeg png pdf xlsx]
  end
end

validate_extension_inclusion %wjpg jpeg png pdf xlsx帮助我<3

票数 0
EN

Stack Overflow用户

发布于 2020-10-07 21:13:31

我试着用'get.mime_type‘学习mime类型,结果是“应用程序/xml”mime类型。我把它添加到validate_mime_type_inclusion中,解决了这个问题

票数 0
EN

Stack Overflow用户

发布于 2021-09-22 02:54:24

嗨,我知道这是一个老帖子,但这是我找到的XLS文件的解决方案。

可以,即使您添加了"application/vnd.ms-excel“来支持xls文件,如果您有条件"validate_mime_type_inclusion”,它也无法上传。

因此,为了修复它,我添加了以下代码"application/x-ole-storage“

代码语言:javascript
复制
  MIME_TYPES = [
    "application/msword",
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    "application/vnd.ms-excel",
    "application/x-ole-storage",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
    ]

 Attacher.validate do
      validate_mime_type_inclusion MIME_TYPES
  end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58081601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档