注: 本片文章参考 redmine
1.
rails upload_file
2. database.yml
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
7.vi app\views\attachment\show.rhtml
8.vi app\views\attachment\upload.rhtml
9.vi app\models\attachment.rb
10.
在根目录创建文件夹 files
1.
rails upload_file
2. database.yml
ruby 代码
- development:
- adapter: sqlite3
- dbfile: db/dev.db
- test:
- adapter: sqlite3
- dbfile: db/test.db
- production:
- adapter: sqlite3
- 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 代码
xml 代码
- <table>
- <thead>
- <th>Filename</th>
- <th>Disk_filename</th>
- <th>Content_type</th>
- <th>Downloads</th>
- <th>Filesize</th>
- </thead>
- <tbody>
- <% Attachment.find(:all).each do |a| %>
- <tr>
- <td><a href="/attachment/download/<%= a.id%>"><%= a.filename %></a></td>
- <td><%= a.disk_filename%></td>
- <td><%= a.content_type%></td>
- <td><%= a.downloads%></td>
- <td><%= a.filesize%></td>
- </tr>
- <% end %>
- </tbody>
- </table>
- <hr />
- <a href=/attachment/upload>upload</a>
8.vi app\views\attachment\upload.rhtml
xml 代码
- <form action="/attachment/create/1" class="tabular" enctype="multipart/form-data" method="post">
- <input id="file" name="file" size="30" type="file" />
- <input name="commit" type="submit" value="Create" />
- form>
ruby 代码
- class Attachment < ActiveRecord::Base
- validates_presence_of :filename
- cattr_accessor :storage_path
- @@storage_path = "#{RAILS_ROOT}/files"
- def validate
- errors.add_to_base :too_long if self.filesize > 5140.kilobytes
- end
- def file=(incomming_file)
- unless incomming_file.nil?
- @temp_file = incomming_file
- if @temp_file.size > 0
- self.filename = sanitize_filename(@temp_file.original_filename)
- self.disk_filename = DateTime.now.strftime("%y%m%d%H%M%S") + "_" + self.filename
- self.content_type = @temp_file.content_type
- self.filesize = @temp_file.size
- end
- end
- end
- def file
- nil
- end
- def before_save
- if @temp_file && (@temp_file.size > 0)
- logger.debug("saving '#{self.diskfile}'")
- File.open(diskfile, "wb") do |f|
- f.write(@temp_file.read)
- end
- end
- end
- def after_destroy
- if self.filename?
- File.delete(diskfile) if File.exist?(diskfile)
- end
- end
- # Returns file's location on disk
- def diskfile
- "#{@@storage_path}/#{self.disk_filename}"
- end
- def increment_download
- increment!(:downloads)
- end
- def self.most_downloaded
- find(:all, :limit => 5, :order => "downloads DESC")
- end
- private
- def sanitize_filename(value)
- just_filename = value.gsub(/^.*(\\|\/)/, '')
- @filename = just_filename.gsub(/[^\w\.\-]/,'_')
- end
- end
在根目录创建文件夹 files