Home > Archive > PERL Modules > September 2006 > Downloading a TIF file using LWP?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Downloading a TIF file using LWP?
|
|
| Mark G. 2006-09-04, 3:57 am |
| Hello.
I am trying to download a TIFF file using the HTTP protocol.
I am using LWP and the following code snippet. Obviously I am
doing something wrong, since the resulting file will not open
in my viewer, and it is approximately 1/2 the size of the same
file if I open it in my browser and save it (double-byte
character probem, perhaps?)
Any suggestions?
Thanks
-Mark
#!/usr/bin/perl
#
use LWP::UserAgent;
$ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1 ");
# Create a request
my $req = HTTP::Request->new(GET =>
'http://www.some_domain.com/some_tiff_file.tif');
$req->content_type('image/tiff');
my $res = $ua->request($req);
if ($res->is_success) {
my $OUTFIL;
open ($OUTFIL, ">test.dat") || die $!;
print $OUTFIL $res->content;
close $OUTFIL;
}
else {
print $res->status_line, "\n";
}
| |
| John Bokma 2006-09-04, 3:57 am |
| "Mark G." <nospam@thanksanyway.org> wrote:
> Hello.
>
> I am trying to download a TIFF file using the HTTP protocol.
> I am using LWP and the following code snippet. Obviously I am
> doing something wrong, since the resulting file will not open
> in my viewer, and it is approximately 1/2 the size of the same
> file if I open it in my browser and save it (double-byte
> character probem, perhaps?)
>
> Any suggestions?
>
> Thanks
> -Mark
>
>
> #!/usr/bin/perl
> #
use strict;
use warnings;
> use LWP::UserAgent;
> $ua = LWP::UserAgent->new;
> $ua->agent("MyApp/0.1 ");
>
> # Create a request
> my $req = HTTP::Request->new(GET =>
> 'http://www.some_domain.com/some_tiff_file.tif');
use example.com for examples
> $req->content_type('image/tiff');
my $url = .... # as above
my $response = $ua->get( $url, :content_file => 'test.tiff' );
$response->is_success or die $response->status_line;
No idea if this fix the issue though, but it makes your code shorter :-D.
--
John Experienced Perl programmer: http://castleamber.com/
Perl help, tutorials, and examples: http://johnbokma.com/perl/
| |
| Sisyphus 2006-09-04, 3:57 am |
|
"Mark G." <nospam@thanksanyway.org> wrote in message
..
..
> if ($res->is_success) {
> my $OUTFIL;
> open ($OUTFIL, ">test.dat") || die $!;
binmode($OUTFIL);
> print $OUTFIL $res->content;
> close $OUTFIL;
> }
> else {
> print $res->status_line, "\n";
> }
>
Seems odd to be calling a tif file "test.dat". Perhaps I've misunderstood
something.
Cheers,
Rob
| |
| Mark G. 2006-09-04, 6:56 pm |
| "Mark G." <nospam@thanksanyway.org> wrote:
>
> Any suggestions?
And the problem was. . ."coding while drowsy."
After a good night's sleep, I switched the output file handle to binary
mode:
binmode $OUTFIL;
And all was well.
Thanks to all who responded.
-Mark
|
|
|
|
|