注: 本片文章参考 redmine
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 代码
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 代码
- <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>
9.vi app\models\attachment.rb
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
-
-
- 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
10.
在根目录创建文件夹 files
分享到:
相关推荐
jquery-fileupload-rails, 用于 Rails的jQuery文件上传集成 Rails 文件上传jQuery-File-Plugin 是一个文件上传插件,由的Tschan 。 jQuery文件上传功能多文件选择。drag&拖放支持。进度栏和jQuery预览图像。 支持...
用于RailsHTML5文件上传器这个gem使用来上传文件。安装在Gemfile中: gem 'rails-uploader'在航线上: mount Uploader :: Engine => '/uploader' 迁移ActiveRecord: $ bundle exec rails g uploader:install用法...
NULL 博文链接:https://mylir.iteye.com/blog/800734
可实现多文件的同时上传,控制文件的格式,数量,同时兼容IE6,7,firefox,易于扩展
NULL 博文链接:https://dendrobium.iteye.com/blog/305938
css和图片 博文链接:https://hideto.iteye.com/blog/197517
自述文件 该自述文件通常会记录启动和运行应用程序所需的所有步骤。 您可能要讲的内容: Ruby版本系统依赖配置数据库创建数据库初始化如何运行测试套件服务(作业队列,缓存服务器,搜索引擎等) 部署说明… 如果您...
NULL 博文链接:https://hlee.iteye.com/blog/360099
jack_up, 在 Rails 中,[DEPRECATED] 轻松AJAX文件上传 从October年8 月开始,已经不推荐使用 另外,对于其他的, 付费和免费的上传工具,JackUp已经被否决了。 JackUp简单AJAX文件在 Rails 中上传。安装修改你的...
文件上传控件支持多个文件选择,拖放支持,进度条,验证和预览图像,音频和视频的jQuery插件。...支持标准的HTML表单文件上传和任何服务器端平台(谷歌应用程序引擎,PHP,Python和Ruby on Rails的,Java等)。
与支持标准HTML表单文件上传的任何服务器端平台(PHP,Python,Ruby on Rails,Java,Node.js,Go等)一起使用。 演示版 产品特点 多文件上传: 允许一次选择多个文件并同时上传。 拖放支持: 允许通过从桌面或...
具有多文件上传、拖拽、进度条和图像预览功能的文件上传插件,支持跨域、分块、暂停恢复和客户端...可与任何服务端平台(如PHP、Python、Ruby on Rails、Java、Node.js、Go等)一起使用,支持标准的HTML表单文件上传。
含有rails的自动完成,上传文件 ,分页,上传文件进度插件
Active Storage 使得在Rails应用中能够很方便上传和引用云服务中的文件,像Amazon S3或Google Cloud Storage,并将这些文件附加到Active Records中。 它还提供用于测试或本地部署的磁盘服务,但重点是云存储。
这是应用程序中使用的一些关键组件的列表: (Web框架) (对象关系数据库) (JavaScript框架) (标记) (Rails文件上传) (基本CSS /设计)设置要求: Ruby(> = 1.9.3) Postgresql(SQLite或MySQL也会在一定...