multipart_progress.rb

Path: lib/action_controller/cgi_ext/multipart_progress.rb
Last Update: Tue Feb 28 15:33:21 UTC 2006

Overview

This module will extend the CGI module with methods to track the upload progress for multipart forms for use with progress meters. The progress is saved in the session to be used from any request from any server with the same session. In other words, this module will work across application instances.

Usage

Just do your file-uploads as you normally would, but include an upload_id in the query string of your form action. Your form post action should look like:

  <form method="post" enctype="multipart/form-data" action="postaction?upload_id=SOMEIDYOUSET">
    <input type="file" name="client_file"/>
  </form>

Query the upload state in a progress by reading the progress from the session

  class UploadController < ApplicationController
    def upload_status
      render :text => "Percent complete: " + @session[:uploads]['SOMEIDYOUSET'].completed_percent"
    end
  end

Session options

Upload progress uses the session options defined in ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS. If you are passing custom session options to your dispatcher then please follow the "recommended way to change session options":wiki.rubyonrails.com/rails/show/HowtoChangeSessionOptions

Update frequency

During an upload, the progress will be written to the session every 2 seconds. This prevents excessive writes yet maintains a decent picture of the upload progress for larger files.

User interfaces that update more often that every 2 seconds will display the same results. Consider this update frequency when designing your progress polling.

Required files

cgi   action_controller/base   action_controller/cgi_process   action_controller/upload_progress  

[Validate]