Class: Fileupload
- Inherits:
-
Object
- Object
- Fileupload
- Defined in:
- lib/fileupload.rb
Instance Method Summary collapse
-
#getname(filestream) ⇒ Object
保证名称不重复.
-
#img_validata(file) ⇒ Object
大小限制,类型限制,都放进模型层中。.
-
#initialize(filepath = "", min = 1, max = 4000) ⇒ Fileupload
constructor
A new instance of Fileupload.
-
#message ⇒ Object
错误提示.
- #upload(file) ⇒ Object
Constructor Details
#initialize(filepath = "", min = 1, max = 4000) ⇒ Fileupload
Returns a new instance of Fileupload.
2 3 4 5 6 7 |
# File 'lib/fileupload.rb', line 2 def initialize(filepath="",min=1,max=4000) @filepath=filepath @message="" @minsize =min*1024 @maxsize=max*1024 end |
Instance Method Details
#getname(filestream) ⇒ Object
保证名称不重复
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/fileupload.rb', line 39 def getname(filestream) img_for = filestream[filestream.length-4, 4] filename = Time.now.strftime("%Y%m%d%h%m%s")<<rand(99999).to_s<<img_for file = @filepath+filename while File.exist?(file) do filename = Time.now.strftime("%Y%m%d%h%m%s")<<rand(99999).to_s<<img_for file = @filepath+filename end filename end |
#img_validata(file) ⇒ Object
大小限制,类型限制,都放进模型层中。
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/fileupload.rb', line 20 def img_validata(file) img_for = /\.(f4v|mp4|mp3)+$/.match(file.original_filename) if img_for.nil? || img_for.blank? @message="错误:影音文件格式不对,只允许上传f4v,mp4,mp3格式!\\n" return false end if file.size<@minsize || file.size>@maxsize @message="错误:影音文件大小错误,只允许#{@minsize}kb~#{@maxsize}kb!\\n" return false end return true end |
#message ⇒ Object
错误提示
34 35 36 |
# File 'lib/fileupload.rb', line 34 def @message end |
#upload(file) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/fileupload.rb', line 9 def upload(file) if self.img_validata(file) sname = self.getname(file.original_filename) File.open(@filepath+sname, "wb") do |f| f.write(file.read) end sname end end |