Rails,Paperclip没有验证内容类型" application / octet-stream"?

时间:2016-12-03 22:22:25

标签: ruby-on-rails paperclip

我使用paperclip上传mime-type" application / octet-stream"的文件,但它们没有正确验证。

在控制器中,当我拨打replay.save!时,我收到以下错误:

Validation failed: r_file has contents that are not what they are reported to be, r_file is invalid, r_file content type is invalid

以下是模型:

class Replay < ApplicationRecord
    has_attached_file :r_file
    validates_attachment_content_type :r_file, content_type: { content_type: "application/octet-stream" }
end

和重放控制器中的create方法:

def create
    @replay = Replay.new(replay_params)
    if @replay.save
        # This never runs because it won't validate.
        puts "REPLAY SAVED."
        redirect_to @replay
    else
        puts "REPLAY NOT SAVED."
        render 'new'
    end
end

我检查了我尝试上传的文件的mime类型,它肯定是"application/octet-stream"类型。 Paperclip只是错误地读取文件类型吗?

编辑:

这是架构:

ActiveRecord::Schema.define(version: 20161203161351) do

  create_table "replays", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.string   "map"
    t.datetime "created_at",          null: false
    t.datetime "updated_at",          null: false
    t.string   "r_file_file_name"
    t.string   "r_file_content_type"
    t.integer  "r_file_file_size"
    t.datetime "r_file_updated_at"
  end

end

1 个答案:

答案 0 :(得分:1)

验证rails Video / Image

中的所有格式
validates_attachment_content_type :media,
      content_type: [
        'image/jpg',
        'image/jpeg',
        'image/pjpeg',
        'image/png',
        'image/x-png',
        'video/avi',
        'video/mov',
        'video/mp4',
        'video/x-flv',
        'video/3gpp',
        'video/quicktime',
        'video/x-msvideo',
        'video/x-ms-wmv',
        'flv-application/octet-stream',
        'application/octet-stream',
        'video/x-flv',
        'video/mpeg',
        'video/mpeg4',
        'video/x-la-asf',
        'video/x-ms-asf'
      ],
        :message => 'file type is not allowed'