Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paperclip Audio file upload

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
like image 375
pritesh Avatar asked Apr 19 '14 12:04

pritesh


People also ask

Where can I upload audio clips?

Google Drive, Dropbox, and Soundcloud are all services that you can use to share your audio on Facebook.

How do you use a paperclip in rails?

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.


1 Answers

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', ...]
like image 142
Rene Avatar answered Nov 14 '22 23:11

Rene