1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #!/usr/bin/perl
use warnings;
use lib 'lib';
use CGI::ProgressBar qw/:standard/;
$| = 1; # Do not buffer output
my $data;
my $hook_called;
my $cgi = CGI->new(\&bar_hook, $data);
if (not $hook_called){
print $cgi->header,
$cgi->start_html( -title=>'A Simple Example', ),
$cgi->h1('Simple Upload-hook Example');
}
print $cgi->start_form( -enctype=>'application/x-www-form-urlencoded'),
$cgi->filefield( 'uploaded_file'),
$cgi->submit,
$cgi->end_form,p;
if ($cgi->param('uploaded_file')){
print 'uploaded_file: '.param('uploaded_file');
}
sub bar_hook {
my ($filename, $buffer, $bytes, $data) = @_;
if (not $hook_called){
print header,
start_html( -title=>'Simple Upload-hook Example', ),
h1('Uploading'),
p(
"Have to read <var>$ENV{CONTENT_LENGTH}</var> in blocks of <var>$bytes</var>, total blocks should be ",
($ENV{CONTENT_LENGTH}/$bytes)
),
progress_bar( -from=>1, -to=>($ENV{CONTENT_LENGTH}/$bytes), -debug=>1 );
$hook_called = 1;
} else {
# Called every $bytes, I would have thought.
# But calls seem to go on much longer than $ENV{CONTENT_LENGTH} led me to believe they ought:
print update_progress_bar;
print "$ENV{CONTENT_LENGTH} ... $total_bytes ... $hook_called ... div="
.($hook_called/$total_bytes)
."<br>"
}
sleep 1;
$hook_called += $total_bytes;
}
print $cgi->hide_progress_bar;
if ($hook_called){
print p('All done after '.$hook_called.' calls');
}
print $cgi->end_html;
exit; |
Partager