上传附件或者图片,通过自己掌握的知识,独立实现了文件的上传和下载,其实并不复杂,只要理解File类的读写操作即可,同时为了让文件保存具有无限增长性(也就是上传文件不重名),使用了随机生成文件名的方法。同时在数据库中采用一个字段,保存文件信息。
通过自己的琢磨,对文件的上传下载有了切身的体会。然后反过来,使用attachment_fu插件。
安装不必说了,google一把就有一大堆。下面按照应用的顺序展开说:
1.创建model,比如attachments.rb
id | int(11)
filename | varchar(255)
content_type | varchar(255)
size | int(11)
width | int(11)
height | int(11)
parent_id | int(11)
thumbnail | varchar(255)
created_at | datetime
updated_at | datetime
具体做法:
script/generate model Attachment
然后在db目录下修改***create_attachments.rb,最终为:
end
class CreateAttachments < ActiveRecord::Migration
def self.up
create_table :attachments do |t|
t.column :filename, :string
t.column :content_type, :string
t.column :size, :integer
t.column :width, :integer
t.column :height, :integer
t.column :parent_id, :integer
t.column :thumbnail, :string
t.timestamps
end
end
def self.down
drop_table :attachments
end
end
然后rake db:migrate
在attachments.rb中写入如下代码:
class Attachment < ActiveRecord::Base
has_attachment :storage => :file_system,
:thumbnails => {:thumb => [20,20], :tiny =>[10,10] }
validates_as_attachment
end
知识点:
a.需要说明的是has_attachment后面的参数比较多。常用的,如果与图片相关,可以讲size之类的参数加入。我的这个案例是任何文件,同时兼顾实用图片的缩略图。缩略图使用两种:thumb和tiny,这两种可以按照你的想象力任意命名。如thumb1和tiny1.都会填写到模型的thumbtail属性值中。
b.validates_as_attachment使用之后的后果。
经常性的不能将附件上传成功。查看原因的时候,development.log,会看到如下错误:
DEPRECATION WARNING: ActiveRecord::Errors.default_error_messages has been deprecated. Please use I18n.translate('activerecord.errors.messages').. (called from default_error_messages at D:/IR2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:24)
在网上查询之后,参照javaeye中的http://lectery.iteye.com/blog/337435 对attachment的源代码修改之后,恢复正常。不过记得需要重启mongrel服务。
c.使用attachment_fu插件之后,具备一个重要的方法,public_filename。他是模型中定义的方法,可以让controller来使用返回文件的原始位置。这个方法作用大大的,可让view中正确的处理附件。同时可以调用缩略图等。
呵呵,还可以使用console进行调测,这是很爽的一件事情。
>> Attachment.find(47).public_filename
=> "/attachments/0000/0047/Water_lilies.jpg"
>> Attachment.find(47).public_filename :thumb
=> "/attachments/0000/0047/Water_lilies_thumb.jpg"
(这里有一个小bug,在public_filename的后面可以跟任意symbol,都可以返回值,而这些返回值对应的文件压根就不存在)
2.编写控制器中action代码,view中的视图代码
action代码中的new:
def new
@attachment = Attachment.new
end
对应new.html.erb
<% form_for(:attachment, :url => { :action => "create" }, :html => { :multipart => true }) do |form| %>
<%= form.file_field :uploaded_data %>
<%= submit_tag "Create" %>
<%end%>
create代码:
def create
@attachable_file = Attachment.new(params[:attachment])
if @attachable_file.save
flash[:notice] = 'Attachment was successfully created.'
else
flash[:notice] = 'Attachment is failure.'
end
end
呵呵,上传附件从此简单
2.使用send_file打开附件
view代码
<%=button_to '下载',:action => "send_file_to_user" %>
点击‘下载’按钮,下载文件,调用send_file_to_user
位于控制器中的send_file_to_user代码
def send_file_to_user
send_file "#{RAILS_ROOT}/public/files/"+'Winter.jpg',
:disposition => "inline",
:filename => "Winter1.jpg"
end
这里需要说明:
inline,表明直接显示出来。如果是图片可以直接显示;如果希望用户下载,改为‘attachment’即可。
filename是用户可以看到的下载文件名,无关紧要。
第一个参数最重要,这里仅仅是一个具体的文件显示案例,可以结合attachment_fu提供的public_filename获取文件名,动态的根据用户需求完成文件的显示或下载。
分享到:
相关推荐
AT_Attachment_with_Packet_Interface_-_7_Volume_3
_storage_emulated_0_android_data_com.tencent.mm_MicroMsg_517174082dbc007f25c5bd836bdd4446_attachment_段润昌_648.wps
ATA接口的详细解读,working draft proposed American National Standard for Information Systems - ATA (ATAttachment) 78页
_storage_emulated_0_android_data_com.tencent.mm_MicroMsg_7cef220aad1703aed0c0f4bb62bea90d_attachment_1_实验四(1) .docx
个人博客:https://blog.csdn.net/MissXy_/article/details/83347968 参考使用;
Information Technology - AT Attachment with Packet Interface - 6 (ATA/ATAPI-6)ATA_ATAPI-6标准规范,驱动开发参考文档
用于LTE中下行物理链路中CRC添加的程序
生成四角、八角、十六角的方位标志。方便做图例。
西门子840d数控系统说明。对方的更多更好
Attachment 1_chazhi.xlsx
very interesting matlab hev model
attachment_doc是一个SquirrelMail插件,允许用户使用其浏览器查看电子邮件中的文档附件。 该插件将文档转换为html格式。 目前支持MSWord(DOC)和可移植文档格式(PDF)!
Attachment_Project 附件项目
Volume 1 defines the register delivered commands used by devices implementing the standard. Volume 2 defines the connectors and cables for physical interconnection between host and storage device, ...
1649480820617212_attachment.cgi
attachment_repo 我要分享的一些文件
Filament winding path generation
Salesforce Attachment mover是用Node.js编写的命令行应用程序,执行3种操作 从CSV文件导入附件 将附件导出到本地zip文件 执行附件的组织迁移 要运行此程序,请打开命令提示符,指向安装应用程序的目录并运行: ...
send_email_attachment
fdafdaafdasfdafdfdsafdadfdsafdsa