Re: getting an image
rschramm@whittman-hart.com
Tue, 15 Jun 1999 13:31:15 -0400
Don't use to_string unless you want to print a textual representation of your
binary data.
Your code should be something like the below example for presenting the image
back to the browser
from a call to a cgi (in an image tag maybe: <img src="/cgi-bin/getimage.pl">?).
This same could be used to save to disk instead if you first open a filehandle
to the file
to save and then print to that filehandle like: print FILEHANDLENAME
$result->content;
Finally, if you are on an NT system, you must set binmode() on either the STDOUT
filehandle
(if you are printing to STDOUT) or to the filehandle you opened to save to (if
you are saving to a file).
Code :
$target_url = 'http://www.foo.com/bar.html';
$mime_type = 'image/gif';
use LWP::UserAgent;
$ua = new LWP::UserAgent;
# Instantiate the request object
$req = new HTTP::Request 'GET' => "$target_url";
$req->header('Accept' => "$mime_type");
# Make the request
$result = $ua->request($req);
if ($result->is_success) {
# Set the MIME type for the browser so they know what is coming,
print "Content-type: $mime_type\n\n";
# and then print the result content.
print $result->content;
} else {
print STDERR "Failure Occured!\n";
}
"Jonathan W. Daniel" <Daniel.71@osu.edu> on 06/15/99 12:12:38 PM
To: libwww-perl@ics.uci.edu
cc: (bcc: Rich Schramm/Whittman-Hart LP)
Subject: getting an image
Hello.
I'm using the following code to "get" info on an image from a server:
use LWP::UserAgent;
$ua = new LWP::UserAgent;
$url = new URI::URL('http://www.domain.com/image.gif');
$header = new HTTP::Headers
'Date' => HTTP::Date::time2str(time), # Convert machine
time to a format suitable for the HTTP server
'Accept' => 'text/html', # Tell the server we can
accept HTML
'Accept' => 'text/plain', # Tell the server we can
accept plain text (.txt files)
'Accept' => 'image/gif', # Tell the server we can
accept GIF files
;
$request = new HTTP::Request('GET', $url, $header);
$response = $ua->request($request);
$response_data = $response->as_string;
print "$response_data\n";
Now how do I "get" the actual file and write it to disk and/or browser?
I assume it is this line I have to change:
$response_data = $response->as_string;
... and then write in binary mode, but... I've gone through my books that
cover LWP but can't find the solution.
thanks,
Jonathan Daniel