gio_Beginer
gio_Beginer

Reputation: 71

how to download `decoded_content`

***UPDATED CODE with resume functionality**

    my $ua = LWP::UserAgent->new;
    $ua->credentials('$ip:80', 'Realm', 'username', 'password');
    my $response = $ua->mirror($url,$newfile);
    if ($response->is_success) {
       print "Download Successfull.";
    }
    else {
        print "Error: " . $response->status_line;
    }

********OLD CODE*****************

    my $ua = LWP::UserAgent->new;
    $ua->credentials('$ip:80', 'Realm', 'username', 'password');
    my $response = $ua->get($url);
    if ($response->is_success) {
       print "Retrieved " .length($response->decoded_content) .
             " bytes of data.";
    }
    else {
        print "Error: " . $response->status_line;
    }

open my $fh, '>encoding(UTF-8)', $tmp;
print {$fh} $response->decoded_content;
close $fh;

if ( -e $tmp ) {
   my $filesize = ( stat $tmp )[9];
   my $origsize = $queue[$rec][1];

   if ( $filesize < $origsize) {
      print "Resuming download";
   ******************************************
  code for resuming the partly downloaded file...
   *******************************************
   }
   else {
      print "File downloaded correctly\n";
   }
}

As i'm newbie to perl, could download decoded_content, though some errors persists. Need to resume the file download, if we have a partial file.

This was the code i've tried, but am not able to know where to start with, hence any quick thoughts in this regard will be of great help indeed. Please help on this.

Upvotes: 0

Views: 1220

Answers (1)

daxim
daxim

Reputation: 39158

See method mirror in LWP::UserAgent. Documentation quote:

This method will get the document identified by $url and store it in file called $filename.

my $response = $ua->mirror($url, $filename); # no single quotes around variables!

See the source code for mirror, it deals correctly with truncated/partially downloaded files.

Upvotes: 1

Related Questions