Ruby on rails 无法使用类型应用程序上载回形针中的不同文件/*

Ruby on rails 无法使用类型应用程序上载回形针中的不同文件/*,ruby-on-rails,ruby-on-rails-5,paperclip,paperclip-validation,Ruby On Rails,Ruby On Rails 5,Paperclip,Paperclip Validation,我正在使用rails 5和ruby 2.4 gem "paperclip", "~> 5.0.0" 我上传的文件的内容类型如下 @content_type=application/octet stream这是来自终端的,所以我在模型中提到了这个内容类型: class CompletedJob < ApplicationRecord # validates :name, presence: true # Make sure the owner's name is present.

我正在使用rails 5和ruby 2.4

gem "paperclip", "~> 5.0.0"
我上传的文件的内容类型如下 @content_type=application/octet stream这是来自终端的,所以我在模型中提到了这个内容类型:

class CompletedJob < ApplicationRecord
  # validates :name, presence: true # Make sure the owner's name is present.
  has_attached_file :file
  validates_attachment :file,
                       :content_type => {content_type: [ "application/octet-stream", 'text/plain', 'text/csv', 'application/vnd.ms-excel']}, message: "is not in CSV format",
                       :size => {in: 0..400.megabytes}
  belongs_to :job
end
但错误仍然是一样的:

由于扩展名不正确,文件未上载

你能告诉我哪里出了问题吗? 提前谢谢

终端输出为:

Processing by Cader::CadersController#update_job as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"QrHTEdY2QhEQQmMI111lqnBglPdNIwWeLBn4aDLeR/2I5Wyxa1JteVyOCZDr9x6DTQYzr/VKpIWkcSNJy8HXYw==", "completed_job"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x007fcba009f808 @tempfile=#<Tempfile:/tmp/RackMultipart20190222-18635-6rkt5h.cfg>, @original_filename="job.cfg", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"completed_job[file]\"; filename=\"job.cfg\"\r\nContent-Type: application/octet-stream\r\n">, "job_id"=>"31122", "reference_name"=>"212 BLOOMER ", "source_type"=>"TUKAcad current edition", "other_source_type"=>"", "final_type"=>"TUKAcad current edition", "other_final_type"=>""}, "commit"=>"upload"}
  User Load (0.6ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` DESC LIMIT 1
  Role Load (0.5ms)  SELECT `roles`.* FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 2 AND (((roles.name = 'cader') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
   (0.3ms)  SELECT COUNT(*) FROM `cader_notifications` WHERE `cader_notifications`.`recipient_id` = 2 AND `cader_notifications`.`read_at` IS NULL
Command :: file -b --mime '/tmp/9dddd5ce1b1375bc497feeb871842d4b20190222-18635-827e75.cfg'
[paperclip] Content Type Spoof: Filename job.cfg (application/octet-stream from Headers, [] from Extension), content type discovered from file command: text/plain. See documentation to allow this combination.
   (0.2ms)  BEGIN
Command :: file -b --mime '/tmp/9dddd5ce1b1375bc497feeb871842d4b20190222-18635-l58du6.cfg'
[paperclip] Content Type Spoof: Filename job.cfg (application/octet-stream from Headers, [] from Extension), content type discovered from file command: text/plain. See documentation to allow this combination.
  Job Load (0.4ms)  SELECT  `jobs`.* FROM `jobs` WHERE `jobs`.`id` = 31122 ORDER BY `jobs`.`id` DESC LIMIT 1
   (0.1ms)  ROLLBACK
Redirected to http://localhost:3000/cader/caders/checkin
Completed 302 Found in 30ms (ActiveRecord: 2.1ms)

曲别针认为你的cfg文件是伪造的。通过将以下内容添加到曲别针配置中,您可以尝试将欺骗检查默认为默认接受文本文件:

Paperclip.options[:content_type_mappings] = {
  :cfg => "text/plain"
}

曲别针认为你的cfg文件是伪造的。通过将以下内容添加到曲别针配置中,您可以尝试将欺骗检查默认为默认接受文本文件:

Paperclip.options[:content_type_mappings] = {
  :cfg => "text/plain"
}

创建一个文件initializers/paperclip.rb并写入:

module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end
在模型中,将验证写为:

validates_attachment  :file,
                        :content_type => {content_type: /\/(?!(php|pl|exe|pm|cfm|asp)$)/},
                        :size => {in: 0..400.megabytes}

创建一个文件initializers/paperclip.rb并写入:

module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end
在模型中,将验证写为:

validates_attachment  :file,
                        :content_type => {content_type: /\/(?!(php|pl|exe|pm|cfm|asp)$)/},
                        :size => {in: 0..400.megabytes}