LWP must communicate through HTTP in the same fashion as a browser, or else
it wouldn't work.  That's what open protocols are for, basically:  to force
differing applications to communicate with each other in exactly the same
way so you can produce predictable behavior.

You might want to check the lwpcook man page, which has an example of
uploading a file through LWP.  I've used the method described there many
times to upload files, and it works fine, regardless of the receiving
application (assuming the receiving application works at all).

Best of luck.

jpt

> -----Original Message-----
> From: Don Smitheimer [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, February 18, 2003 9:53 AM
> To: [EMAIL PROTECTED]
> Subject: File Upload: Fh reference from browser
> 
> 
> Hi All,
> 
> I am using Perl v5.6.1, LWP v5.65.
> 
> My original problem started when I tried to create a LWP 
> program to upload files to a CGI application package--to 
> automate tasks performed by cumbersome and tedious browser 
> interfaces I must do during my adminstration duties with this 
> application.
> 
> I looked at the Perl source code and noticed they first check 
> for a Fh reference of the uploaded file, otherwise the upload 
> is not successful.  I coded up my LWP program--my first time 
> using the upload capability of LWP--but was not successful.  
> So I decided to modify their code slightly to dump the 
> request data.  I noticed then that in fact when using a 
> browser the request shows some kind of Fh reference (in fact, 
> something like this: 'File1' => [bless( 
> \*{'Fh::fh00001C%3A\\My Documents\\whatever.txt'}, 'Fh' ) ).  
> But that's not what I see when using LWP.
> 
> So I coded up a simple upload CGI app and upload program to 
> get things working at a simple level.  But I have not been 
> successful with that either.  The upload CGI app works with a 
> browser, and the data dump shows a Fh ref, but it doesn't 
> work with the LWP program.
> 
> So now I have two problems: a) I can't figure out how to 
> automate my task via LWP and, b) I can't get my simple LWP to 
> upload a file.
> 
> I have read in the docs (man pages and LWP book) that LWP 
> includes the contents of the file in the HTTP response.  Is 
> that what a browser does?  If so, where does the ref to Fh 
> come from?  If I am doing something wrong with the LWP code, 
> I'd appreciate pointing it out. If my code is functionally 
> correct, then how do I convince the original CGI app that it 
> has received an Fh reference? (it's not practical, and it is 
> illegal, for me to modify the source code of the orignal CGI app).
> 
> Any help in this is greatly appreciated.
> 
> I have included the source and output from my simple upload 
> functions below for your reference.
> 
> Thanks much,
> 
> Don Smitheimer
> 
> 
> ======Simple upload CGI app=======
> #!/usr/local/bin/perl
> 
> use CGI;
> use Data::Dumper;
> 
> my $q = new CGI();
> 
> my $form = qq|
>          <P>Enter Filename:</P> 
>          <FORM ENCTYPE="multipart/form-data" METHOD="POST" 
> ACTION="upload.cgi">
>                 <INPUT TYPE="FILE" NAME="File1"> <INPUT 
> TYPE="SUBMIT" NAME="Subm
> it1"> </FORM> |;
> 
> open LOG, ">>/usr/local/bb/log/upload.log";
> 
> $Method = $q->request_method();
> 
> print $q->header();
> 
> print $q->start_html;
> 
> if ( $Method eq 'POST') {
> print LOG "\n";
> 
> print LOG "==================In POST Method ------------------\n";
> print LOG Dumper($q);
> print LOG "\n";
> $fh = $q->param('File1');      # Is this supposed to be a 
> filehandle or the string?  I thought string but with browser it's Fh.
> print LOG "\$fh = $fh\n";
> print LOG Dumper($fh);
> 
>     my $buffer;
>     my $bytesread = 0;
>     open (OUTFILE,">>/usr/local/bb/log/uploaded.file") || die 
> "can't open file $!";
>     while ($bytesread=read($fh,$buffer,1024)) {
>      print LOG "bytesread = $bytesread\n";
>        print OUTFILE $buffer;
>     }
>     close OUTFILE;
> } else {
> print LOG "\n";
> 
> print LOG "==================In NOT POST Method ------------------\n";
> print LOG Dumper($q);
> print LOG "\n";
> print $form;
> }
> close LOG;
> --------------------------------------------------------------
> ----------------
> 
> =========Simple upload LWP program==========
> #!/usr/local/bin/perl
> # upload.pl
> # Don L. Smitheimer
> # Jan 31, 2003
> 
> use strict;
> use LWP::UserAgent;
> use LWP::Debug qw(+);
> use Data::Dumper;
> 
> my $TRUE = (1 == 1);
> my $FALSE = ! $TRUE;
> my $Host   = "server";
> my $Today;
> my $PathToOutput = "./";
> 
> my $logfile = "./upload.log";
>  
>   my $True = ( 1 == 1 );
>   my $False = ! $True;
> 
>   my $HostURL = $Host . "some server somewhere";
>   my $URI = "http://$HostURL";;
>   my $Response;
>   my $ua;
>   my $Code;                     # Response code from server
>   my $File = "/usr/local/bb/pgms/bbupload/batchcreate.txt";
>   
>   $ua = LWP::UserAgent->new;
>   
>   $Response = $ua->post("$URI/upload.cgi",
>               [ 'File1' => [$File],
>               ],
>               'Content_Type' => 'form_data'
>              );
> 
>   open LOG, ">>$logfile";
>   print LOG Dumper($Response);
>   close LOG;
> ------------------------------------------------------------
> 
> =====Output of CGI app from browser=====
> ==================In NOT POST Method ------------------
> $VAR1 = bless( {
>                  '.header_printed' => '1',
>                  '.charset' => 'ISO-8859-1',
>                  '.parameters' => [],
>                  '.fieldnames' => {}
>                }, 'CGI' );
> 
> 
> ==================In POST Method ------------------
> $VAR1 = bless( {
>                  '.tmpfiles' => {
>                                   '3' => {
>                                            'info' => {
>                                                        
> 'Content-Disposition' => 'form-data; name="File1"; 
> filename="E:\\perl_code\\test.txt"',
>                                                        
> 'Content-Type' => 'text/plain'
>                                                      },
>                                            'name' => bless( 
> do{\(my $o = '/usr/tmp/CGItemp63197')}, 'TempFile' )
>                                          }
>                                 },
>                  '.header_printed' => '1',
>                  '.charset' => 'ISO-8859-1',
>                  'Submit1' => [
>                                 'Submit Query'
>                               ],
>                  '.parameters' => [
>                                     'File1',
>                                     'Submit1'
>                                   ],
>                  '.fieldnames' => {},
>                  'File1' => [
>                               bless( 
> \*{'Fh::fh00001E%3A\\perl_code\\test.txt'}, 'Fh' )
>                             ]
>                }, 'CGI' );
> 
> $fh = E:\perl_code\test.txt
> $VAR1 = bless( \*{'Fh::fh00001E%3A\\perl_code\\test.txt'}, 'Fh' );
> bytesread = 16
> --------------------------------------------------------------
> 
> =====Output of CGI app from LWP program=====
> ==================In POST Method ------------------
> $VAR1 = bless( {
>                  '.header_printed' => '1',
>                  '.charset' => 'ISO-8859-1',
>                  '.parameters' => [
>                                     'File1'
>                                   ],
>                  '.fieldnames' => {},
>                  'File1' => [
>                               
> '/usr/local/bb/pgms/bbupload/batchcreate.txt'
>                             ]
>                }, 'CGI' );
> 
> $fh = /usr/local/bb/pgms/bbupload/batchcreate.txt
> $VAR1 = '/usr/local/bb/pgms/bbupload/batchcreate.txt';
> ---------------------------------------------------------
> 
> Hope this helps.
> 

Reply via email to