每天一剂Rails良药之upload_progress

本文介绍如何使用Rails的upload_progress插件实现文件上传状态监听及进度条显示。通过配置插件并在控制器中定义相应方法,可以轻松实现上传进度的实时反馈。
今天来看看使用upload_progress插件监听文件上传status以及创建AJAX上传progress bar
Requirements:[url]http://sean.treadway.info/articles/2005/07/18/upload-progress-checklist[/url]

安装好该插件以及Requirements后,我们就可以在controller里使用了:
[code]
class DocumentController < ApplicationController
upload_status_for :create

def create
# ... Your document creation action
end
end

class Ducoment2Controller < ApplicationController
upload_status_for :create, :status => :custom_status

def create
# ... Your document creation action
end

def custom_status
# ... Override this action to return content to be replaced in the status container
render :inline => "<%= upload_progress.completed_percent rescue 0 %> % complete", :layout => false
end
end
[/code]
我们在view中这样使用:
[code]
<%= form_tag_with_upload_progress({:action => 'create'}, {
:begin => 'alert("Upload beginning")',
:finish => 'alert("Upload finished")'}) %>
<%= file_field 'document', 'file' %>
<%= submit_tag "Upload" %>
<%= upload_status_tag %>
<%= end_form_tag %>
[/code]

BTW:Rails官方插件就介绍到这儿了,补充贴一下Global Changelog:
[quote]
* Moved acts_as_taggable, browser_filters, and simply_helpful into legacy -- all have either been deprecated or rolled into Rails core already [DHH]

* Resource feeder turned out to be too restrictive and too hard to customize; replace it by atom_feed_helper [DHH]

* Extract dynamic scaffolding plugin from core. #7700 [Josh Peek]

* Start a global changelog for the official Rails plugins. [Jeremy Kemper]
[/quote]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值