0

***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.

animuson
  • 53,861
  • 28
  • 137
  • 147
  • Maybe you should start reading a book. How about [this one](http://www.amazon.com/Learning-Perl-Randal-L-Schwartz/dp/1449303587/ref=sr_1_1?ie=UTF8&qid=1337074010&sr=8-1) – matthias krull May 15 '12 at 09:27
  • After reading you can [edit](http://stackoverflow.com/posts/10597630/edit) your question to fit stackoverflow. – matthias krull May 15 '12 at 09:36

1 Answers1

1

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.

daxim
  • 39,270
  • 4
  • 65
  • 132
  • Daxim thanks for your very quick help. With the way mentioned above i got the files downloaded to my local directory. but its yelling out with the error "print() on closed filehandle $fh at download.pl" now i've to resume a partly downloaded file from the my temporary directory. – gio_Beginer May 16 '12 at 04:57
  • Thanks again Daxim, some how i overlooked your response. the mirror() worked well and suits my requirement. i've added the modified code :) – gio_Beginer May 16 '12 at 09:57