donno20 has asked for the wisdom of the Perl Monks concerning the following question:

Dear all,
I am writing a ftp client to download a file. How to make my program sexy enough to show user the progress of their download?
I used cpan module NET::FTP and cannot found there are some API do this.

Replies are listed 'Best First'.
Re: FTP downloading progress bar
by robartes (Priest) on Apr 11, 2003 at 07:13 UTC
    From the docs:
    hash ( [FILEHANDLE_GLOB_REF],[ BYTES_PER_HASH_MARK] ) Called without parameters, or with the first argument false, hash +marks are suppressed. If the first argument is true but not a referen +ce to a file handle glob, then \*STDERR is used. The second argument +is the number of bytes per hash mark printed, and defaults to 1024. I +n all cases the return value is a reference to an array of two: the f +ilehandle glob reference and the bytes per hash mark.
    So, $ftp->hash(\*STDOUT,1024); or similar should do the trick for you.

    CU
    Robartes-

Re: FTP downloading progress bar
by submersible_toaster (Chaplain) on Apr 11, 2003 at 07:25 UTC

    To expand on ++robartes very correct suggestion. To pull a progress bar from this you will need to know a few other things.

    What method you're using to display a progress bar is beyond the scope of this document (YES! I have been dying to use that in a sentence). Ahem. Let us presume you use 1024 byte hashmarks and count the hashes as they are printed. You must then normalise the progress in bytes ($num_of_hashes * 1024) , with your $total_dl_size like

    $normalised_progress = ( ($num_of_hashes * 1024) / $total_dl_size );

    $normalised_progress will be a value from 0 to 1 where 1 indicates a completed download. In reality you will need to limit this because of the inprecision(sp.) introduced by hashmarking in blocks of 1024 bytes. Oh yes, and be aware that the above code is not divide.by.zero safe!!


    I can't believe it's not psellchecked
Re: FTP downloading progress bar
by DrManhattan (Chaplain) on Apr 11, 2003 at 09:41 UTC
Re: FTP downloading progress bar
by tachyon (Chancellor) on Apr 11, 2003 at 11:16 UTC

    wget style progress bar may also be of assistance although you may have to hack Net::FTP to use it effectively. I use it with raw socket code network traffic and don't know if Net::FTP supports a callback.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: FTP downloading progress bar
by zentara (Cardinal) on Apr 11, 2003 at 15:49 UTC
    Do a search on http://perlmonks.org for "download progress" and you will find some nice examples of using gtk, and tk. Also check out cpan, in the Tk modules, for Tk::ProgressBar and Tk::ProgressIndicator. Here is a simple snippet for one. Just figure out a way to count your received bytes and put it in the Repeater sub.
    #!/usr/bin/perl use Tk; use Tk::ProgressIndicator; my $l_MainWindow = MainWindow->new(); my $l_ProgressIndicator = $l_MainWindow->ProgressIndicator ( '-current' => 0, '-limit' => 200, '-increment' => 10, '-height' => 20, '-width' => 400 ); $l_AfterID = $l_MainWindow->repeat (500, sub {Repeater();}); $l_Counter = 0; $l_ProgressIndicator->pack(); Tk::MainLoop(); ################################################### sub Repeater { $l_ProgressIndicator->configure ('-current' => $l_Counter); if (($l_Counter += 10) > 200) { $l_MainWindow->afterCancel ($l_AfterID); } } #####################################################
Re: FTP downloading progress bar
by donno20 (Sexton) on Apr 15, 2003 at 08:11 UTC
    Please refer to http://www.perlmonks.org/index.pl?node_id=250499 for further discussion on CGI.
    thanks ^_^