yourkeylady
Programmer
I am trying to transfer files with LWP.
The mime types are jpg,png,swf,txt
I have tried LWP::UserAgent, and the images are jumbled.
The swf doesn't have any content.
Thanks
Tricia
The mime types are jpg,png,swf,txt
I have tried LWP::UserAgent, and the images are jumbled.
The swf doesn't have any content.
Code:
# I was hoping to write in chunks as recieved
my $ua = new LWP::UserAgent;
$ua->agent("MyApp/0.1");
$ua->max_size( 1200);
foreach(keys %file_list){
my $expected_length;
my $bytes_received = 0;
my$content_type = "text/plain";
if(open(FILE,">$file_out{$_}")){
if($_ eq 'dat' || $_ eq 'txt'){
}else{
binmode FILE;
if($file_list{$_} =~ /\.png$/){
$content_type = "image/png ";
}elsif($file_list{$_} =~ /\.jpg$/){
$content_type = "image/jpeg";
} elsif($file_list{$_} =~ /\.swf$/){
$content_type = "application/x-shockwave-flash";
}
}
print qq~\n<BR>$content_type~;
}else{
$ok = 1;
$pkg::scripterror .= "\n<br>CANNOT WRITE: $file_out{$_} $!";
next;
}
my $req = HTTP::Request->new(POST => $file_list{$_},sub {
my($chunk, $res) = @_;
$bytes_received += length($chunk);
unless (defined $expected_length) {
$expected_length = $res->content_length || 0;
}
if ($expected_length) {
printf "%d%% - ",
100 * $bytes_received / $expected_length;
}
print "\n<BR>$bytes_received bytes received\n";
print FILE $chunk;
if($expected_length == $bytes_received){
close(FILE);
}
}
);
$req->content_type($content_type);
my $res = $ua->request($req);
#$method, $uri, $header, $content
#my $res = $ua->request(HTTP::Request->new("POST", $file_list{$_},$h,
print qq~\n<BR>fetching $file_list{$_} ~.length($res->content);
#$output .= qq~\n<BR> writing $file_out{$_}~;
}
Thanks
Tricia