Krishna Vyas Krishna Vyas - 4 months ago 69
Ruby Question

Paperclip: How to upload Powerpoint presentation files

How to upload powerpoint presentation files and other office related documents using rails paperclip processor.

Here is my code.
In mime_types.rb

Mime::Type.register "application/vnd.openxmlformats-officedocument.wordprocessingml.document", :docx
Mime::Type.register "application/vnd.openxmlformats-officedocument.presentationml.presentation", :pptx
Mime::Type.register "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", :xlsx


In my model asset.rb

has_attached_file :attachment, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :attachment, :content_type => ["application/pdf", "application/zip", "application/x-zip", "application/x-zip-compressed","application/octet-stream","image/jpg", "image/png", "image/jpeg", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"]


I am receiving this error
Assets attachment content type is invalid
Assets attachment is invalid

Answer

In your Model

validates_attachment_content_type :attachment, :content_type => ["application/pdf", "application/zip", "application/x-zip", "application/x-zip-compressed","application/octet-stream","application/vnd.ms-office","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","application/vnd.openxmlformats-officedocument.presentationml.presentation","image/jpg", "image/png", "image/jpeg"]

In your mime_types.rb

Mime::Type.register "application/pdf", :pdf
Mime::Type.register "application/xls", :xls
Rack::Mime::MIME_TYPES.merge!({
    ".xls"     => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",  
    ".xlsx"     => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    ".ppt"     => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
    ".pptx"     => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
    ".doc"     => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    ".docx"     => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
})
Comments