Rails、Paperclip 未验证内容类型“应用程序/八位字节流"? [英] Rails, Paperclip not validating content type "application/octet-stream"?

查看:52
本文介绍了Rails、Paperclip 未验证内容类型“应用程序/八位字节流"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用回形针上传 MIME 类型应用程序/八位字节流"的文件,但它们没有正确验证.

I'm using paperclip to upload files of the mime-type "application/octet-stream", but they aren't validating properly.

在控制器中,当我调用 replay.save! 时,出现以下错误:

In the controller, when I call replay.save!, I get the following error:

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方法:

and the create method in the replay controller:

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" 类型.回形针只是错误地读取了文件类型?

I checked the mime-type of the file I'm trying to upload, and it is definitely of type "application/octet-stream". Is Paperclip just reading the file type incorrectly?

这是架构:

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

推荐答案

验证 rails Video/Image 中的所有格式

Validate all formats in 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'

这篇关于Rails、Paperclip 未验证内容类型“应用程序/八位字节流"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆