Saturday, August 05, 2006

Perl - Issues

Problems encountered on 2006-07-31:

1. Today, for some reason, we are seeing the variables being passed via GET in URLs. This actually sounds more normal, I guess the question is why it wasn't happening before??!!

Solution
- Started calling CGI scripts using:
exec ( "perl my.cgi @sendArgs" );
instead of:
print "Location:my.cgi?arg1=$val1\n\n";


2. IE during file upload
my $uploadFile = param( 'uploadFile' );
sends the entire path, not just the file name.
C:\path\dreaming.zip

Firefox just sends the filename.

Must support both situations.

Solution
Here is the file spec, which varies from IE to Firefox.
my $uploadFile = param( 'uploadFile' );

So I extract the file name
my @pathArr = split( /\\/, $uploadFile );
my $pathEltCnt = scalar( @pathArr );
my $fileName = $pathArr[$pathEltCnt-1];

Some functions need the file spec as provided by the browser, in whatever form.
my $uploadInfo = uploadInfo( $uploadFile );
my $uploadType = $uploadInfo -> { 'Content-Type' };

Note that for opening I use $fileName, but for actual reading I use the original file spec.
open ( UPFILE, "> $dirName/$fileName" ) || die("Cannot open($fileName): $!");
binmode( UPFILE );
my ( $data, $chunk );
my $fileSize = 0;
while ( $chunk = read ( $uploadFile, $data, 1024 ) ) {
print UPFILE $data;
$fileSize += $chunk;
}
close ( UPFILE ) || die("Cannot close($fileName): $!");


No comments: