On Thursday, July 11, 2002, at 01:30 PM, timj_moore wrote:
> Hi, > > I'm having trouble getting file uploads to work. Part of the problem > is I don't understand where CGI fits in. The documentation is saying > the uploads are done with CGI, but I've looked at the CGI file upload > documentation but to me it looks like the actual upload bit doesn't > require CGI. Same goes for the file upload sample. CGI seems to be > used to create the upload form but after that the upload is just > using perl's builtin 'read' function. Hi- The CGI module is used to parse the POSTed data and separate the upload from the rest of the form data. I believe it creates a file in /tmp/ or C:\WINDOWS\TEMP\, which is then read with read. > > Or have I got the wrong end of the stick? > > Anyway, what I've got is an ASP page that has a regular form that > posts the upload form data to another page (the upload field is > called 'uploadedfile'). The 2nd page accepts the upload form data > correctly but when 'read' is called I get nothing in my scalar > (though the 'read' loop correctly loops until the file has been read). > > My code is as follows... > ---- > my $length = $Request->{TotalBytes}; > print "TotalBytes = " . $length . "<br>"; > > my $fileup = $Request->{FileUpload}{uploadedfile}; > my $filehandle = $fileup->{FileHandle}; > my $PostedData = "FFF"; > print "PostedData = " . $PostedData . "<br>"; > print "Length = " . length($PostedData) . "<br>"; > while (read($filehandle, $PostedData, 1024)) > { > print "a"; > # data from the uploaded file read into $PostedData > } > > my $ContentType = $fileup->{ContentType}; > print "ContentType = " . $ContentType . "<br>"; > > print "PostedData = " . $PostedData . "<br>"; > print "Length = " . length($PostedData) . "<br>"; > ---- > > What I get as a result is something like this... > > TotalBytes = 46840 > PostedData = FFF > Length = 3 > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa > ContentType = application/pdf > PostedData = > Length = 0 > > The FFF and the a's are just so I can test that PostedData is being > changed and that the read is looping correctly and reading the right > ammount of data. The incoming file data is the right size and the > file is correctly being determined as a PDF. > > Yet PostedData ends up empty. ?? > > Or am I missing something else and the scalar is being filled but you > just can't display the contents with a print (though PDF's are mostly > printable)? I'm not too hot on Perl so scalars confuse me a little. I believe was is happening is this: Each time you call read(), it fetches up to 1024 bytes (1kb, as you set). Since each time read is called, it erases $PostedData and then writes up to 1024 bytes into it, you can only end up with the last 1024 bytes. HOWEVER, the last time read is called, it reaches EOF, so the last thing $PostedData is set to is undef. Here is a modified version of the code that I think will work (untested): my $length = $Request->{TotalBytes}; print "TotalBytes = " . $length . "<br>"; my $fileup = $Request->{FileUpload}{uploadedfile}; my $filehandle = $fileup->{FileHandle}; my $PostedData = ""; print "PostedData = " . $PostedData . "<br>"; print "Length = " . length($PostedData) . "<br>"; my $in_buffer; while (read($filehandle, $in_buffer, 1024)) { print "a"; $PostedData .= $in_buffer; # Equivalent to $PostedData = $PostedData . $in_buffer, except .= is shorter and faster. # data from the uploaded file read into $PostedData } my $ContentType = $fileup->{ContentType}; print "ContentType = " . $ContentType . "<br>"; print "PostedData = " . $PostedData . "<br>"; print "Length = " . length($PostedData) . "<br>"; # As always, watch for line breaks. __END__ HTH, --Quentin > > Ultimately the PostedData goes via SWIG into a C++ app, but that's > another matter, I just need it to get filled first. > > P.S. I don't called 'use CGI;' or anything, though I have tried > without success. > > > > Tim. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]