Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why does LWP mirror function not transfer my swf

Status
Not open for further replies.

yourkeylady

Programmer
Jan 8, 2004
63
US
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.

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
 
I tried the mirror function but it did not work

Thanks
Tricia
 
Images and Flash movies are binary files, so you need to treat them as such (i.e. use binmode when writing to a filehandle).

Here's a simpler example of getting/saving an image with LWP::Simple:

Code:
use LWP::Simple;
my $jpg = get "[URL unfurl="true"]http://somesite.com/image.jpg";[/URL]

open (OUT, ">./local_copy.jpg");
binmode OUT; # prepares it for binary writing
print OUT $jpg;
close (OUT);

And then local_copy.jpg would come in clear. Without the binmode it would look all glitchy.
 
Some of the files may be large. So I have to recieve it in chunks. I used binmode in my code.

Thanks
Tricia
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top