随笔-167  评论-65  文章-0  trackbacks-0
rails中把文件上传到文件夹中,一般有图片,rar文件等等,试着找了些rails file upload的plugins (fleximage,attachment_fu,paperclip.....),但是最后发现都不是太好用,最后利用原始ruby api实现了,实现步骤:

rhtml:
    <form name="photo" enctype="multipart/form-data" action="/upload/path" method="post">
    Photo <input type="file" name="image"  id="image" size="30"  />
    <input type="submit" name="upload" value="Upload"  class="button" />
    </form>

controller:
path = 'public/data'
File.open(path, "wb") { |f| f.write(params[:image].read) }

that's all,简单吧。。注意在用File类的一些方法时,需要require 'ftools'

ref:
http://www.tutorialspoint.com/ruby-on-rails-2.1/rails-file-uploading.htm



write by feng
posted on 2009-06-19 19:19 fl1429 阅读(737) 评论(0)  编辑  收藏 所属分类: Rails

只有注册用户登录后才能发表评论。


网站导航:
 
已访问数:
free counters