Scott Dolan
2010-11-10 15:53:24 UTC
I need to add a file upload with a progress bar page to my website that I developed with Mason. I am already implemented a standard file upload with a progress bar using CGI (code below). The script uses a callback hook function my $q = new CGI (\&hook); sub hook{ ...}; However, this code does not work within mason. It seems the mason component is only executed after the complete file has been received which prevents me from sending responding back with the status of current upload process.
Is there a way to modify how mason recieves the files so, I can add a progress bar. I am figuring, I am going to have to do some low end development to get this feature. How do I go about developing such a feature? I think this would be valueable feature and necessary feature to have in the mason framework especially to make mason viable for new development. I am willing to do the work but, I need some direction.
Scott
732-300-9956
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp "fatalsToBrowser";
use Data::Dumper;
# Make a file upload hook.
my $q = new CGI (\&hook);
# This is the file upload hook, where we can update our session
# file with the dirty details of how the upload is going.
sub hook
{
my ($filename,$buffer,$bytes_read,$file) = @_;
# Get our sessid from the form submission.
my ($sessid) = $ENV{QUERY_STRING};
$sessid =~ s/[^A-F0-9]//g;
# Calculate the (rough estimation) of the file size. This isn't
# accurate because the CONTENT_LENGTH includes not only the file's
# contents, but also the length of all the other form fields as well,
# so it's bound to be at least a few bytes larger than the file size.
# This obviously doesn't work out well if you want progress bars on
# a per-file basis, if uploading many files. This proof-of-concept only
# supports a single file anyway.
my $length = $ENV{'CONTENT_LENGTH'};
my $percent = 0;
if ($length > 0)
{
# Don't divide by zero.
$percent = sprintf("%.1f", (( $bytes_read / $length ) * 100) );
}
# Write this data to the session file.
open (SES, "> ./files/$sessid.session");
print SES "$bytes_read:$length:$percent";
close (SES);
open( DUM, ">> ./files/dumper.txt");
print DUM "hook " . Dumper($ENV{REQUEST_METHOD});
close( DUM );
Is there a way to modify how mason recieves the files so, I can add a progress bar. I am figuring, I am going to have to do some low end development to get this feature. How do I go about developing such a feature? I think this would be valueable feature and necessary feature to have in the mason framework especially to make mason viable for new development. I am willing to do the work but, I need some direction.
Scott
732-300-9956
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp "fatalsToBrowser";
use Data::Dumper;
# Make a file upload hook.
my $q = new CGI (\&hook);
# This is the file upload hook, where we can update our session
# file with the dirty details of how the upload is going.
sub hook
{
my ($filename,$buffer,$bytes_read,$file) = @_;
# Get our sessid from the form submission.
my ($sessid) = $ENV{QUERY_STRING};
$sessid =~ s/[^A-F0-9]//g;
# Calculate the (rough estimation) of the file size. This isn't
# accurate because the CONTENT_LENGTH includes not only the file's
# contents, but also the length of all the other form fields as well,
# so it's bound to be at least a few bytes larger than the file size.
# This obviously doesn't work out well if you want progress bars on
# a per-file basis, if uploading many files. This proof-of-concept only
# supports a single file anyway.
my $length = $ENV{'CONTENT_LENGTH'};
my $percent = 0;
if ($length > 0)
{
# Don't divide by zero.
$percent = sprintf("%.1f", (( $bytes_read / $length ) * 100) );
}
# Write this data to the session file.
open (SES, "> ./files/$sessid.session");
print SES "$bytes_read:$length:$percent";
close (SES);
open( DUM, ">> ./files/dumper.txt");
print DUM "hook " . Dumper($ENV{REQUEST_METHOD});
close( DUM );