一:建立表(用 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 HTML 1.0 Transitional//EN" "w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
文件
上传测试 <% for column in Attach.content_columns %> <%= column.human_name %> | <% end %>
<% for attach in @attaches %>
<% for column in Attach.content_columns %> <%=h attach.send(column.name) %> | <% end %>
<% end %>
文件上传
<% form_tag({:action => :add}, {:multipart => true}) do -%> 文件名称 1:<%= 文件名称 2:<%= 文件名称 3:<%= 文件名称 4:<%= 文件名称 5:<%= file_field_tag file_field_tag file_field_tag file_field_tag file_field_tag "uploadFile[]" "uploadFile[]" "uploadFile[