`
open2ye
  • 浏览: 99472 次
  • 性别: Icon_minigender_1
  • 来自: 温州
社区版块
存档分类
最新评论

rails 文件上传

阅读更多
注: 本片文章参考 redmine

1.
rails upload_file
2.  database.yml
ruby 代码
  1. development:  
  2.   adapter: sqlite3  
  3.   dbfile: db/dev.db  
  4.   
  5. test:  
  6.   adapter: sqlite3  
  7.   dbfile: db/test.db  
  8.   
  9. production:  
  10.   adapter: sqlite3  
  11.   dbfile: db/prod.db  

3.
ruby script/generate model attachment filename:string disk_filename:string content_type:string created_on:datetime downloads:integer filesize:integer
4.
rake db:migrate
5.
ruby script\generate controller attachment show upload

6.vi app\controllers\attachment_controller.rb
ruby 代码
class AttachmentController < ApplicationController
  def show
  end

  def upload
   
  end
 
  def create
    Attachment.create(:file => params[:file])
    redirect_to :action => "show"
  end
 
  def download
    @attachment = Attachment.find(params[:id])
    @attachment.increment_download

    send_file @attachment.diskfile, :filename => @attachment.filename
  end
end


7.vi app\views\attachment\show.rhtml
xml 代码
 
  1. <table>  
  2.   <thead>  
  3.     <th>Filename</th>  
  4.     <th>Disk_filename</th>  
  5.     <th>Content_type</th>  
  6.     <th>Downloads</th>  
  7.     <th>Filesize</th>  
  8.   </thead>  
  9.   <tbody>  
  10.     <% Attachment.find(:all).each do |a| %>  
  11.     <tr>  
  12.     <td><a href="/attachment/download/<%= a.id%>"><%= a.filename %></a></td>  
  13.     <td><%= a.disk_filename%></td>  
  14.     <td><%= a.content_type%></td>  
  15.     <td><%= a.downloads%></td>  
  16.     <td><%= a.filesize%></td>  
  17.   </tr>  
  18.     <% end %>    
  19.   </tbody>  
  20. </table>  
  21.   
  22. <hr />  
  23. <a href=/attachment/upload>upload</a>  

8.vi app\views\attachment\upload.rhtml
xml 代码
 
  1. <form action="/attachment/create/1" class="tabular" enctype="multipart/form-data" method="post">  
  2. <input id="file" name="file" size="30" type="file" />  
  3. <input name="commit" type="submit" value="Create" />  
  4. <!---->form>  
9.vi app\models\attachment.rb
ruby 代码
 
  1. class Attachment < ActiveRecord::Base  
  2.   validates_presence_of :filename  
  3.     
  4.   cattr_accessor :storage_path  
  5.   @@storage_path = "#{RAILS_ROOT}/files"  
  6.     
  7.   def validate  
  8.     errors.add_to_base :too_long if self.filesize > 5140.kilobytes  
  9.   end  
  10.   
  11.     def file=(incomming_file)  
  12.         unless incomming_file.nil?  
  13.             @temp_file = incomming_file  
  14.             if @temp_file.size > 0  
  15.                 self.filename = sanitize_filename(@temp_file.original_filename)  
  16.                 self.disk_filename = DateTime.now.strftime("%y%m%d%H%M%S") + "_" + self.filename  
  17.                 self.content_type = @temp_file.content_type  
  18.                 self.filesize = @temp_file.size  
  19.             end  
  20.         end  
  21.     end  
  22.       
  23.     def file  
  24.      nil  
  25.     end  
  26.       
  27.     def before_save  
  28.         if @temp_file && (@temp_file.size > 0)  
  29.             logger.debug("saving '#{self.diskfile}'")  
  30.             File.open(diskfile, "wb"do |f|   
  31.                 f.write(@temp_file.read)  
  32.             end  
  33.         end  
  34.     end  
  35.       
  36.     def after_destroy  
  37.         if self.filename?  
  38.             File.delete(diskfile) if File.exist?(diskfile)  
  39.         end  
  40.     end  
  41.       
  42.     # Returns file's location on disk  
  43.     def diskfile  
  44.         "#{@@storage_path}/#{self.disk_filename}"  
  45.     end  
  46.     
  47.   def increment_download  
  48.     increment!(:downloads)  
  49.   end  
  50.       
  51.     def self.most_downloaded  
  52.         find(:all:limit => 5, :order => "downloads DESC")   
  53.     end  
  54.       
  55.   private  
  56.   def sanitize_filename(value)  
  57.       just_filename = value.gsub(/^.*(\\|\/)/, '')   
  58.       @filename = just_filename.gsub(/[^\w\.\-]/,'_')   
  59.   end  
  60.       
  61. end  
10.
在根目录创建文件夹 files
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics