今天来看看使用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]
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]
本文介绍如何使用Rails的upload_progress插件实现文件上传状态监听及进度条显示。通过配置插件并在控制器中定义相应方法,可以轻松实现上传进度的实时反馈。

被折叠的 条评论
为什么被折叠?



