2006-11-08
整合File-Column和Rmagick功能实现图片上传
关键字: 组件应用
在网站制作过程中,图片上传以及图片的大小调整是经常会用到的一个功能!
Rails结合几个plug-in可以说很智能的做到了这一点
做了一个简单的例子,系统在Windows平台上运行
1.上网下载file-column-0.3.1.tar.gz 和rmagick-win32-1.13.0_IM-6.2.9-3.zip (我当前的最新版本,到下述站点下载 http://rubyforge.org/projects/rmagick/ Linux下版本是RMagick-1.14.1.tar.gz)
2.安装rmagick,执行zip包里面的exe文件,同时把安装路径放到path环境变量里面去,否则可能会报CORE_RL_magick_.dll找不到的错误
3.安装file-column到app的vendor目录里,直接copy过去就行
4.建立一个存放路径的model,在数据库中建立Entry数据库
并生成相应的scaffold:
ruby script/generate scaffold Entry upload
4.修改model,并限制只能图片上传
5.修改_form.rhtml
6.修改new.rhtml
7.修改show.rhtml
Rails结合几个plug-in可以说很智能的做到了这一点
做了一个简单的例子,系统在Windows平台上运行
1.上网下载file-column-0.3.1.tar.gz 和rmagick-win32-1.13.0_IM-6.2.9-3.zip (我当前的最新版本,到下述站点下载 http://rubyforge.org/projects/rmagick/ Linux下版本是RMagick-1.14.1.tar.gz)
2.安装rmagick,执行zip包里面的exe文件,同时把安装路径放到path环境变量里面去,否则可能会报CORE_RL_magick_.dll找不到的错误
3.安装file-column到app的vendor目录里,直接copy过去就行
引用
以下的文件配置基本上按照官方提供的sample来进行,算是用中文整合一下,谈不上原创
4.建立一个存放路径的model,在数据库中建立Entry数据库
并生成相应的scaffold:
ruby script/generate scaffold Entry upload
4.修改model,并限制只能图片上传
class Entry < ActiveRecord::Base
validates_format_of :image,
:with=>/^.*(.jpg|.JPG|.gif|.GIF)$/,
:message => "你只能上传JPG或则GIF的图片文件"
file_column :image, :magick => {
:versions => { "thumb" => "50x50", "medium" => "640x480>" }
}
end
5.修改_form.rhtml
<%= error_messages_for 'entry' %> <!--[form:entry]--> <p><label for="entry_image">Image</label><br/> <%= file_column_field 'entry', 'image' %></p> <!--[eoform:entry]-->
6.修改new.rhtml
<h1>New entry</h1> <%= start_form_tag 'create',:multipart => true%> <%= render :partial => 'form' %> <%= submit_tag "Create" %> <%= end_form_tag %> <%= link_to 'Back', :action => 'list' %>
7.修改show.rhtml
<% for column in Entry.content_columns %> <p> <b><%= column.human_name %>:</b> <%=h @entry.send(column.name) %> <br> 原始大小: <%= image_tag url_for_file_column 'entry', 'image' %> <br> thumb: <%= image_tag url_for_file_column 'entry', 'image' ,'thumb'%> <br> medium: <%= image_tag url_for_file_column 'entry', 'image' ,'medium'%> </p> <% end %> <%= link_to 'Edit', :action => 'edit', :id => @entry %> | <%= link_to 'Back', :action => 'list' %>
评论
mrunix
2007-09-14
我可以用rmagick和file_column.但是当我点edit编辑先前存在的记录时,系统提示:
大家有没有遇到这样的问题?谢谢!
Do not know how to handle a string with value 'C:\Documents and Settings\Ryan\桌面\0-Inbox\United Colors of Benetton - Spring-Summer 2007 Collection.files\ben_play.gif' that was passed to a file_column. Check if the form's encoding has been set to 'multipart/form-data'.
RAILS_ROOT: E:/Aptana/test/script/../config/..
Application Trace | Framework Trace | Full Trace
#{RAILS_ROOT}/vendor/plugins/file_column/lib/file_column.rb:52:in `assign'
#{RAILS_ROOT}/vendor/plugins/file_column/lib/file_column.rb:584:in `image='
D:/Tools/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1672:in `send'
D:/Tools/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1672:in `attributes='
D:/Tools/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1671:in `each'
D:/Tools/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1671:in `attributes='
D:/Tools/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1591:in `update_attributes'
#{RAILS_ROOT}/app/controllers/admin_controller.rb:39:in `update'
大家有没有遇到这样的问题?谢谢!
lgn21st
2007-08-23
Javeeye的附件上传功能就支持一次上传多个附件。
lgn21st
2007-08-23
似乎并不难找呀!
simon_lin
2007-08-23
哪位兄弟能不能把file-column-0.3.1.tar.gz 共享出来,我怎么没找到它的下载的地方。
wtb
2007-07-11
kevinye 写道
有没有谁做过用file_column实现 多张照片同时上传的,最好能AJAX
一样的道理吧,我觉得。
在表中多加几个字段,模式中多定义几个列就是了。
dohiker
2007-06-18
请教,我上传文件时怎么才能自定义保存的文件的名字和存放路径?谢谢。
kevinye
2007-06-08
有没有谁做过用file_column实现 多张照片同时上传的,最好能AJAX
nowa
2007-04-22
请教楼主,我的ImageMagick和RMagick都装好了,测试了都没有问题。可是为什么我怎么安装file-column都没有用的?
一直报错:
undefined method `file_column' for PhotosController:Classn
一直报错:
undefined method `file_column' for PhotosController:Classn
mathsfan
2007-04-18
上面的三个错误是我通过ruby script/plugin install http://opensvn.csie.org/rails_file_column/plugins/file_column/命令来安装的,刚才用LZ说的下载file-column-0.3.1.tar.gz 包的话却没有发生这三个错误,怪```
mathsfan
2007-04-18
我在model中magick参数会发生uninitialized constant FileColumn::Magick的错误,要去掉才可以用哈,不知道为什么?
另外,在显示图片时,网页中图片的路径是entry/image/development/13/17.jpg,而实际在APP存储中并没有development文件夹,这个该怎么解决?
最后,我按上面说的把filename.gsub!(/[^a-zA-Z0-9\.\-\+_]/,"_")给注释掉以后确实有的中文可以存了,但有的中文就会发生Invalid argument - ./public/entry/image/tmp/的错误,这个又什么原因?
另外,在显示图片时,网页中图片的路径是entry/image/development/13/17.jpg,而实际在APP存储中并没有development文件夹,这个该怎么解决?
最后,我按上面说的把filename.gsub!(/[^a-zA-Z0-9\.\-\+_]/,"_")给注释掉以后确实有的中文可以存了,但有的中文就会发生Invalid argument - ./public/entry/image/tmp/的错误,这个又什么原因?
秦朝古月
2007-01-15
file_column上传中文文件名的文件时,汉字变成“_”的解决办法
编辑file_column.rb文件
把方法 self.sanitize_filename(filename)中的
filename.gsub!(/[^a-zA-Z0-9\.\-\+_]/,"_")
给注释掉。
编辑file_column.rb文件
把方法 self.sanitize_filename(filename)中的
filename.gsub!(/[^a-zA-Z0-9\.\-\+_]/,"_")
给注释掉。
suave
2006-11-24
请问如何获得上传文件的大小呢?相对相传文件改名后再进行保存应该如何处理?
airport
2006-11-09
file_column上传大写后缀名文件报错的解决办法
编辑file_column.rb文件
把:FileUtils.mv(local_file_path, new_local_file_path) unless new_local_file_path == local_file_path
修改成:FileUtils.mv(local_file_path, new_local_file_path) unless new_local_file_path.downcase == local_file_path.downcase
编辑file_column.rb文件
把:FileUtils.mv(local_file_path, new_local_file_path) unless new_local_file_path == local_file_path
修改成:FileUtils.mv(local_file_path, new_local_file_path) unless new_local_file_path.downcase == local_file_path.downcase
capitain
2006-11-09
有办法加入水印否
airport
2006-11-09
关于Rmagick的一些小Tip
"thumb" => "50x50" : 表示默认宽度50,高度自适应
"thumb" => "50x50!" : "!"表示强制50*50
"thumb" => "50x50>" : ">"表示如果图片本来就小,就不用放大
"thumb" => "50x50" : 表示默认宽度50,高度自适应
"thumb" => "50x50!" : "!"表示强制50*50
"thumb" => "50x50>" : ">"表示如果图片本来就小,就不用放大
lighter
2006-11-08
和以前用ssh做上传下载的代码量相比,不在同一个级别的:少,简洁
melin
2006-11-08
参考一下,以前没有做成功
- 浏览: 51325 次
- 来自: 大连

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
Velocity用户手册---中文 ...
现在用velocity的少了把,不过类似的模板可是越来越多了
-- by airport -
Velocity用户手册---中文 ...
雪中送炭,收了,谢了!!!
-- by chamborghini -
Velocity用户手册---中文 ...
GOOD!
-- by iRoyce -
Velocity用户手册---中文 ...
谢谢!!非常好!!
-- by wxl4040 -
整合File-Column和Rmagic ...
我可以用rmagick和file_column.但是当我点edit编辑先前存在的 ...
-- by mrunix






评论排行榜