How can we restrict file uploads with valid file type but invalid content-type - Rails

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

In my Rails web application, I have to upload audio and video files and for validating against invalid file types , I have used jquery-validation engine and I could do the same successfully. But, if I create a text file and change the extension from .txt to .mp3, e.g. test.txt to test.mp3, it will be taken as valid by jquery validation engine as the file extension is valid for an audio.

I want to check the content type also. When I opened the test.mp3 in a player, it showed me an error message Stream contains no data. I want this kind of validation to be performed in the interface. Is it possible in Rails?

I m using,

Rails 3.2.13

Ruby 2.0.0-dev

Hope anyone can help me out. Thanks :)-

Answers

https://github.com/thoughtbot/paperclip https://github.com/thoughtbot/paperclip

class User < ActiveRecord::Base
  attr_accessible :avatar
  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"

  validates_attachment :avatar, 
  :presence => true, :content_type => { :content_type => "image/jpg" },
  :size => { :in => 0..500.kilobytes }

end

Hope it helps.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/18674295/how-can-we-restrict-file-uploads-with-valid-file-type-but-invalid-content-type

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils