Re: "Hotmail" like browsing local machine and sending attach

Marc Langheinrich (marclang@cs.washington.edu)
Wed, 14 Apr 1999 15:29:33 +0900


On Tue, Apr 13, 1999 at 10:42:23PM -0500, Nathaniel Good wrote:
> I can't see how they can force the browser to open a "browse" window from
> a cgi-script. How does the browser "know"? Can LWP be used to do this?
I haven't used hotmail, so I don't know who their particular solution looks
like, but this sounds like "file upload" to me. 

See ftp://ftp.isi.edu/in-notes/rfc1867.txt

<FORM ENCTYPE="multipart/form-data" ACTION="http://your.server/upload.cgi"
      METHOD=POST>
 What is your name? <INPUT TYPE="text" NAME="username">
 File to process: <INPUT NAME="userfile" TYPE="file">
<INPUT TYPE="submit" VALUE="Send File">
</FORM>

You should be able to do this with LWP as well (i.e. simulating the
response a browser would send given such a form), although I haven't tested
this:

 use LWP::UserAgent;
 
 $ua = new LWP::UserAgent;

 my $req = new HTTP::Request
              'POST','http://your.server/upload.cgi';
	      
 my $boundary="AaB03x"; # some random string	      
 $req->content_type('multipart/form-data, boundary=$boundary');

 open (THEFILE, "file.txt") or die "Could not open file: $!\n";
 my $file = join ("", <THEFILE>);
 close THEFILE;

 my $content= <<ENDOFCONTENT;
 --$boundary
 content-disposition: form-data; name="username"

 John Doe
 --$boundary
 content-disposition: form-data; name="userfile"; filename="file.gif"
 Content-Type: image/text

 $file
 --$boundary
 ENDOFCONTENT

 $req->content($content);

 my $res = $ua->request($req);
 print $res->as_string;


or something like that. There might be nicer ways to assemble the actual
content. Maybe CGI.pm does offer some help here to construct such multipart
messages...

hth

Marc
-- 
Marc Langheinrich
marclang@cs.washington.edu