回形针音频文件上传 [英] Paperclip Audio file upload

查看:82
本文介绍了回形针音频文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用回形针 gem 上传文件.我想上传不同类型的文件,如 pdf、doc、视频和音频.我对模型中的文件类型进行了验证.对于 doc、pdf 和 Video,它可以正常工作,但不能用于音频文件.请帮忙.我的模型

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

推荐答案

这适用于任何类型的文件

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

我运行了 MP3 文件,它返回了

I have run in on MP3 file and it returned

audio/mpeg

因此,如果您只想验证某些内容类型,您可以将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', ...]

这篇关于回形针音频文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆