I am using paperclip gem to upload files. I want to upload different kinds of files like pdf, doc, video and audio. I have validation for file type in my model. For doc, pdf and Video it is working but it is not wroking for audio file. please help. My model
class Xyz < ActiveRecord::Base
attr_accessible :email, :name, :avatar, :CategoryID
has_attached_file :avatar
validates_attachment_content_type :avatar, :content_type => ['video/mp4','video/avi','Audio/mp3','application/pdf',"application/pdf","application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"text/plain"]
#validates_attachment_content_type :avatar, :content_type => ['audio/mp3']
end
This is the error I got...
1 error prohibited this xyz from being saved:
Avatar content type is invalid
Google Drive, Dropbox, and Soundcloud are all services that you can use to share your audio on Facebook.
Setting Up PaperclipTo set up Paperclip, first we need to install the ImageMagick dependency. Paperclip uses ImageMagick to resize images after upload. If you are using another system, you can get download and install instructions from the ImageMagick website. Run bundle install to finish it up.
This will work for any type of the file
validates_attachment_content_type :avatar, :content_type => /.*/
You can also discover exact content type of the file with command
file -i path/to/file # or
file --mime-type path/to/file
I have run in on MP3 file and it returned
audio/mpeg
So if you want to validate only some set of content types you can add 'audio/mpeg' to the list
validates_attachment_content_type :avatar, :content_type => [ ..., 'audio/mpeg', ...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With