Devstack » Articles » File Uploads with Apache2::ASP

File Uploads with Apache2::ASP

One area in which Apache2::ASP really shines is with file uploads.

The reason for this is that file uploads are important for the new web and the interactivity that users expect. File uploads should be as simple to handle as anything else.

Simple File Uploads

To get basic file uploads working, follow these steps:

XML Config

Edit your conf/apache2-asp-config.xml and make sure you have the following:

<configuration>
  ...
  <web>
    ...
    <media_manager_upload_root>@ServerRoot@/MEDIA</media_manager_upload_root>
    ...
  </web>
  ...
</configuration>

Folder Permissions

Your MEDIA folder must be writable by your webserver process.

Supposing your webserver runs as apache, do this:

sudo chown apache:apache -R MEDIA
sudo chmod 0755 MEDIA

The Upload Form

Create an ASP script with a form such as the following:

<form
  action="/handlers/my.MediaManager?mode=create&uploadID=<%= int(rand() * 10000) %>"
  method="post"
  enctype="multipart/form-data">
  
  Select File: <input type="file" name="some_file" />
  <input type="submit" value="Upload File" />
</form>

The Upload Handler

The form submits to /handlers/my.MediaManager, which Apache2::ASP will map to the package my::MediaManager (which you must now create):

package my::MediaManager;

use strict;
use warnings 'all';
use base 'Apache2::ASP::MediaManager';
use vars __PACKAGE__->VARS;

sub after_create
{
  my ($s, $context, $Upload) = @_;
  
  return $Response->Redirect(
    "/success.asp?new_file=" . $Server->URLEncode( $Upload->filename_only )
  );
}# end after_create()

1;# return true:

That $Upload object is an instance of Apache2::ASP::UploadHookArgs and looks like this:

$VAR1 = bless( {                               
                 'new_file' => '/usr/local/dstack/www.example.com/MEDIA/httpd.conf',
                 'content_length' => 1008,
                 'filename_only' => 'httpd.conf',
                 'percent_complete' => '87.40',
                 'data' => undef,
                 'total_expected_time' => 0,
                 'elapsed_time' => '0.0459108352661133',
                 'time_remaining' => 0,
                 'length_received' => 0,
                 'link_to_file' => '/media/httpd.conf',
                 'upload' => bless( {
                                      'filename' => 'httpd.conf',
                                      'upload_filename' => 'httpd.conf'
                                    }, 'Apache2::ASP::Test::UploadObject' )
               }, 'Apache2::ASP::UploadHookArgs' );

Where Files are Stored

By default, the files are stored right there under your application root, in the MEDIA directory.

Of course this can be changed.