Ruby 多文件上传并写入数据库

一:建立表(用Ruby的 script/generate model attach) 顺便把模型也生成
打开 db/migrage/007_create_attaches.rb 修改成
class CreateAttaches < ActiveRecord::Migration
def self.up
    create_table :attaches, :force => true do |t|
      t.column :name,        :string # 附件名称
      t.column :url,         :string # 资源路径
      t.column :types,       :string # 附件类型,存扩展名
      t.column :size,        :integer # 附件大小
      t.column :last_update, :integer # 更新时间
    end
end

def self.down
    drop_table :attaches
end
end

执行 rake db:migrate 会自动创建表到指定的数据库

二:新建立一个UploadController 其内容如下(只是测试用,没有去美化)
class UploadController < ApplicationController

def index
    @attach_pages, @attaches = paginate :attaches, :order => 'last_update DESC', :per_page => 10
end

def add
    begin
      # 因为是多文件上传,所以这里要循环
      params[:uploadFile].each do | file |
        if file.kind_of?(StringIO) || File.exist?(file)
    # 调用自定义的模型来上传文件,传入session_id 来重命名文件,保证文件不重名
    attach = Attach.new(file, session.session_id)
    attach.save
    end
      end
    rescue
      raise
    end
    redirect_to :action => "index"
end

end

三:修改刚才自动生成的模型 Attach
class Attach < ActiveRecord::Base

def initialize(upload, session_id)
    file_upload(upload, session_id) # 执行文件上传
    super()
    self.name = @name
    self.url   = @url
    self.types = @type
    self.size = @size
    self.last_update = @last_update
end


# 文件上传
def file_upload(upload, session_id)
    time_now = Time.now
    @init_dir = "upload/#{time_now.strftime('%Y-%m-%d')}"

    begin
      FileUtils.mkpath(upload_path) unless File.directory?(upload_path)
      if upload.kind_of?(StringIO)
        upload.rewind
      end
   
      origName = upload.original_filename
      baseName = File.basename(origName, ".*")    # 取得文件名字
      extName = File.extname(origName).downcase # 取得文件扩展名
      tmpName = "_1"
      tmpExtName = "_" + time_now.to_i.to_s + extName
     
      # 重命名
      baseName = session_id.nil? ? Digest::MD5.hexdigest(baseName).to_s : session_id.to_s
      baseName = baseName.upcase
      endName = baseName + tmpExtName
     
      # 生成不重复的文件名
      while File.exist?(upload_path(endName))
        arr = endName.scan(/[a-zA-Z0-9_]+/)[0].split("_")
    endName = 2 == arr.size ? (baseName + tmpName + tmpExtName ) : (baseName + "_" + arr[1].succ.to_s + tmpExtName )
      end
     
      # 将文件写入磁盘
      File.open(upload_path(endName), "wb") { |f| f.write(upload.read) }

      @name = origName
      @url = "/#{@init_dir}/#{endName}"
      @type = (extName.delete ".")
      @size = upload.size
      @last_update = time_now.to_i

    rescue
      raise
    end

end

# 生成绝对路径
def upload_path(file = nil)
    "#{RAILS_ROOT}/public/#{@init_dir}/#{file.nil? ? '' : file}"
end
end

四:修改views/upload/index.rhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>文件上传测试</title>
</head>

<body>

<!-- 用于显示已经上传的文件 -->
<table border="1">
<tr>
<% for column in Attach.content_columns %>
    <th><%= column.human_name %></th>
<% end %>
</tr>

<% for attach in @attaches %>
<tr>
<% for column in Attach.content_columns %>
    <td><%=h attach.send(column.name) %></td>
<% end %>
</tr>
<% end %>
</table>



<!-- 用[]命名是为了Ruby得到的是一个文件数组 -->
<h1>文件上传</h1>
<% form_tag({:action => :add}, {:multipart => true}) do -%>

文件名称1:<%= file_field_tag "uploadFile[]" -%> <br/><br/>
文件名称2:<%= file_field_tag "uploadFile[]" -%> <br/><br/>
文件名称3:<%= file_field_tag "uploadFile[]" -%> <br/><br/>
文件名称4:<%= file_field_tag "uploadFile[]" -%> <br/><br/>
文件名称5:<%= file_field_tag "uploadFile[]" -%> <br/><br/>

<%= submit_tag "文件上传", :disable_with => "文件上传中..." , :name => "btnSave", :id => "btnSave" %>

<% end %>
</body>
</html>

代码运行效果如下:
Ruby 多文件上传并写入数据库


你可能感兴趣的:(多文件上传)