NAME

Apache2::ASP::UploadHandler - Base class for Handlers that process file uploads

SYNOPSIS

Place the following code into /handlers/MyUploader.pm:

package MyUploader;

use strict;
use base 'Apache2::ASP::UploadHandler';

# Override any methods we need to:
sub upload_end
{
  my ($s, $Session, $Request, $Response, $Server, $Application, $Upload) = @_;
  shift(@_);
  $s->SUPER::upload_end( @_ );
  
  $Response->Redirect("/index.asp?status=done");
}# end upload_end()

1;# return true:

Then, create a web pages with a form that uploads a file to /handlers/MyUploader:

<html>
...
<form method="POST" enctype="multipart/form-data">
  <input type="file" name="filename">
  <input type="submit" value="Click Here to Upload">
</form>
...
</html>

After the upload has finished, "MyUploader" will redirect you to /index.asp?status=done (where you might display a message of some kind).

DESCRIPTION

This package provides the Apache2::ASP environment with the ability to process file uploads while they are happening. Through the use of libapreq2 we expose trigger points that will be called at specific times during a file upload.

OVERRIDABLE METHODS

upload_start( $self, $Session, $Request, $Response, $Server, $Application, $Upload )

Called just before upload_hook() is called for the first time. If you need to do any kind of setup or double-checking, this is the time to do it.

upload_end( $self, $Session, $Request, $Response, $Server, $Application, $Upload )

Called just after upload_hook() is called for the last time. If you need to do any kind of cleanup or redirect the user, this is the time to do it.

upload_hook( $self, $Session, $Request, $Response, $Server, $Application, $Upload )

Called each time Apache reads in a chunk of bytes from the client during the upload.

ARGUMENTS

All methods - upload_start(), upload_end() and upload_hook() are called with the same arguments. They are listed here:

$Session

The global $Session object. See Apache2::ASP::Session for details.

$Request

The global $Request object. See Apache2::ASP::Request for details.

$Response

The global $Response object. See Apache2::ASP::Response for details.

$Server

The global $Server object. See Apache2::ASP::Server for details.

$Application

The global $Application object. See Apache2::ASP::Application for details.

$Upload

The $Upload argument is a hashref with the following structure:

$VAR1 = {
  'upload'              => APR::Request::Param object,
  'content_length'      => 5333399,
  'percent_complete'    => '99.99',
  'data'                => 'The data that was received by the server in this "chunk"',
  'total_expected_time' => 3,
  'elapsed_time'        => '3.04842114448547',
  'time_remaining'      => 0,
  'length_received'     => 0,
};

In an attempt to explain this in more detail, each element is listed below:

upload

An APR::Request::Param object. Use this object to retrieve information about the uploaded file (i.e. filehandle, MIME type, etc).

content_length

The size of the entire HTTP request as specified by $ENV{CONTENT_LENGTH}.

percent_complete

A float that indicates the percent of the upload we have received so far.

data

The string of bytes that was received from the client in the most recent chunk.

total_expected_time

Total number of seconds we expect this entire upload operation to take.

elapsed_time

Total number of seconds since we started this upload.

time_remaining

Total number of seconds remaining for this upload.

length_received

Total number of bytes we have received from the client at this point.

HOW TO SAVE UPLOADED FILE DATA

Of course the whole point of uploading files is to save them on the server.

Do it like this:

# Get our filehandle:
my $in_filehandle = $Upload->{upload}->upload_fh;

# Create a local file to write to:
open my $out_filehandle, '>', '/path/to/save/file';

# Make MSWin32 happy:
binmode($out_filehandle);
binmode($in_filehandle);

# Read from one filehandle and write to the other:
while( my $line = <$in_filehandle> )
{
  print $out_filehandle $line;
}# end while()

# Finish up:
close($in_filehandle);
close($out_filehandle);

AUTHOR

John Drago jdrago_999@yahoo.com

COPYRIGHT AND LICENSE

Copyright 2007 John Drago, All rights reserved.

This software is free software. It may be used and distributed under the same terms as Perl itself.