Examples: Upload with Progress Bar

This is about as simple as it gets.

Select a File: 
 
 
File List:
[ No Files Found ]

The form posts to a subclass of Apache2::ASP::MediaManager with the following code:

package MediaManager;

use strict;
use warnings 'all';
use base 'Apache2::ASP::MediaManager';
use vars qw(
  $Session $Application
  $Server  $Response
  $Request $Config
  $Form
);

sub after_create
{
  $Response->Redirect("/examples/upload.asp");
}

sub after_delete
{
  $Response->Redirect("/examples/upload.asp");
}

1;# return true:

There is some JavaScript code that gets executed by the "Submit" button that retrieves upload progress information from the following handler:
package UploadProgress;

use strict;
use base 'Apache2::ASP::FormHandler';
use vars qw(
  $Request $Response
  $Server  $Session
	$Config  $Application
	$Form
);

#==============================================================================
sub run
{
  my ($s, $asp) = @_;
  $Session->{percent_complete} ||= 0;
	$Session->{time_remaining} ||= 0;

  $Response->Write( $Session->{percent_complete} );
  $Response->Flush;
}# end run()

1;# return true:
Then in httpd.conf we update the following line:
  # Change this:
  PerlTransHandler  Apache2::ASP::TransHandler
  # To this:
  PerlTransHandler  TransHandler
And create a file under your /lib folder named TransHandler.pm with the following contents:
package dstack::TransHandler;

use strict;
use warnings 'all';
use base 'Apache2::ASP::TransHandler';

sub handler : method
{
  my ($class, $r) = @_;

  # Change all /media/* requests to be /handlers/dstack/MediaManager?file=.* requests:
  if( $r->uri =~ m@^/media/.+@ )
  {
    # Fixup /media/* URL requests:
    return Apache2::Const::DECLINED()
      unless $r->uri =~ m/^\/media\/.+/;
    my ($file) = $r->uri =~ m/^\/media\/([^\?]+)$/;
    
    my @args = ( "file=$file" );
    if( $r->args )
    {
      push @args, $r->args;
    }# end if()
    
    # Fixup the uri and args:
    $r->uri( '/handlers/dstack/MediaManager' );
    $r->args( join '&', @args );
    
    # Send the request on down the line to the next handler:
    return Apache2::Const::DECLINED();
  }
  else
  {
    return $class->SUPER::handler( $r );
  }# end if()
}# end handler()

1;# return true:

 

That's all there is to it. If you want to do something special with your uploaded file, just add some code to your after_create and after_update methods.

Also see the documentation for Apache2::ASP::MediaManager.