From b5419dc0e819550b5db0bc18579d8243477c53f2 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 12 Nov 2009 19:32:46 -0800 Subject: JSON compatible with Ry Dahl's Ajax.Pull + example Ry posted about it here: http://rubyforge.org/pipermail/mongrel-users/2007-July/003747.html This is technically superior compared to all the existing interfaces since it only requires only one, long-running client-side HTTP request. I would imagine it's possible for a JavaScript programmer to be capable of even removing the need for an extra request entirely, I'm not that programmer... --- .../app/controllers/files_controller.rb | 24 +++++++---- .../rails_app-2.3.4/app/views/files/pull.html.erb | 46 ++++++++++++++++++++++ 2 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 examples/rails_app-2.3.4/app/views/files/pull.html.erb (limited to 'examples/rails_app-2.3.4/app') diff --git a/examples/rails_app-2.3.4/app/controllers/files_controller.rb b/examples/rails_app-2.3.4/app/controllers/files_controller.rb index 004c13e..a0fdbb1 100644 --- a/examples/rails_app-2.3.4/app/controllers/files_controller.rb +++ b/examples/rails_app-2.3.4/app/controllers/files_controller.rb @@ -1,14 +1,15 @@ require 'digest/sha1' class FilesController < ApplicationController - # used by streaming upload progress + # used by jQuery streaming upload progress def new - if request.post? - size, hexdigest = _read_sha1_size - msg = "Successfully upload file (size: #{size}, sha1: #{hexdigest})" - flash[:notice] = msg - redirect_to "/files/new" - end + _sha1_flash_self + end + + # based on Ry Dahl's streaming AJAX pull: + # http://rubyforge.org/pipermail/mongrel-users/2007-July/003747.html + def pull + _sha1_flash_self end def index @@ -51,4 +52,13 @@ private [ file.size, digest.hexdigest ] end + def _sha1_flash_self + if request.post? + size, hexdigest = _read_sha1_size + msg = "Successfully upload file (size: #{size}, sha1: #{hexdigest})" + flash[:notice] = msg + redirect_to :action => params[:action] + end + end + end diff --git a/examples/rails_app-2.3.4/app/views/files/pull.html.erb b/examples/rails_app-2.3.4/app/views/files/pull.html.erb new file mode 100644 index 0000000..275704d --- /dev/null +++ b/examples/rails_app-2.3.4/app/views/files/pull.html.erb @@ -0,0 +1,46 @@ + + + + + streaming upload progress test + <%= javascript_include_tag 'ajax_pull/prototype-1_5_1.js' %> + <%= javascript_include_tag 'ajax_pull/ajax_pull.js' %> + <%= javascript_include_tag 'ajax_pull/upload_progress.js' %> + + +<% +upid = "#{Time.now.to_i}.#{rand}" +act = { :action => 'pull', :upload_id => upid } +opt = { + :multipart => true, + :target => 'upload-target', + :onsubmit => "UploadProgress.begin('#{upid}')" +} +-%> + + <% if flash[:notice] %> +
<%= flash[:notice] %>
+ <% end %> +

<%= link_to "#{upid} (single)", + :action => 'status', :upload_id => upid %>

+

<%= link_to "#{upid} (stream)", + "/progress?long&upload_id=#{upid}" %>

+ + <% form_tag(act, opt) do %> +

<%= file_field_tag :data %>

+

<%= submit_tag :Upload %>

+ <% end %> + +

Below is the results div

+
(nothing)
+ +

Below is the progress bar div

+
+ +

Below is the debug list

+
    + + + + + -- cgit v1.2.3-24-ge0c7