VC + + Download file get Progress sample

Source: Internet
Author: User
Tags current time
The code is as follows Copy Code

Get current time stamp
int Getunixtime ()
{
CTime time = Ctime::getcurrenttime ();
return (int) time. GetTime ();
}


CString Formatunit (double nbytes)
{
char* Units[5] = {"B", "KB", "MB", "GB", "TB"};
int i;
for (i = 0; ((int) nbytes) >= 1024 && i < 4; i++)
Nbytes/= 1024;
CString strresult;
Strresult.format ("%.2f%s", Nbytes, Units[i]);
return strresult;
}

DWORD winapi DownloadFile (lpvoid pparam)
{
 cinstallonlinedlg* pwnd = (cinstallonlinedlg*) AfxGetApp ()- >m_pMainWnd;
 char                FILEBUF[512];
 CInternetSession    netsession;
 CStdioFile          *ftargfile = NULL;
 CString             Szfile (Download_url), FileSize, Kbin, kbsec, NewName, Perc, Strleft, strright;
 int ndownloaded = 1;
 try
 {
  pwnd->setleftstring ("Verifying download Address ...");
  ftargfile = Netsession.openurl (szfile, 1, internet_flag_transfer_binary | Internet_flag_reload);
  int filesize = Ftargfile->seektoend ();
  ftargfile->seektobegin ();

  //creates a new destination file  
  cfile fdestfile in the current directory (Ftargfile->getfilename (), CFile:: Modecreate | Cfile::modewrite | Cfile::typebinary);
  int byteswrite;    //bytes written to file  
  int pos = 0;  & nbsp;    //Current progress bar position  
  double kbsec; //record seconds, speed (kb/sec)  
   int secs;
 
  pwnd->setfilename (Ftargfile->getfilename ());
  pwnd->m_ Progress. SetRange32 (0, filesize);

  int nstarttime = Getunixtime ();
  int nlastoutputtime = 0;
  cstring strfilesize = Formatunit (filesize);
  while (byteswrite = Ftargfile->read (filebuf))   //Read file  
  {
   //the number of seconds based on the start time compared to the current time
   int ncurrenttime = Getunixtime ();
    secs = Ncurrenttime-nstarttime;
   secs = secs = = 0? 1:secs;
   pos = pos + byteswrite;                 //Set new progress bar position  
   fdestfile.write (Filebuf, byteswrite);   //write the actual data to the file  
   pwnd->m_progress. SetPos (POS);

   if ((ncurrenttime-nlastoutputtime) > 1)
   {
     kbsec = pos/secs;                  /Get how many downloads per second (KB)
    cstring strrecv = Formatunit (POS);
     cstring strspeed = Formatunit (kbsec);
    strleft.format ("Download Progress:%s/%s", Strrecv.getbuffer (), Strfilesize.getbuffer ());
    pwnd->setleftstring (strleft);
    strright.format ("Download speed:%s/s", Strspeed.getbuffer ());
    pwnd->setrightstring (strright);
    nlastoutputtime = ncurrenttime;
   }
  }
  //Download complete, close file  
  fdestfile.close ();
 }

catch (CInternetException *ie)
{
CString strerror;
TCHAR error[255];

Ie->geterrormessage (Error, 255); Get error messages
strerror = error;

  pwnd->setleftstring (strerror);
  pwnd->setrightstring ("");
  if (ftargfile)
   delete ftargfile;
  ie->delete ();                    // Remove the exception object to prevent leaks  
  pwnd->setdownloadstatus (FALSE);
  pwnd->getdlgitem (Idc_button1)->enablewindow (TRUE);
  return 0;
 }
 if (ftargfile)
  delete ftargfile;
 if (ndownloaded = 1)
 {
   Pwnd->setleftstring ("Download complete, click" Install "!");
  pwnd->m_btn. Loadbitmaps (Idb_btn_install_normal);
  pwnd->m_btn. SizeToContent ();
  pwnd->setdownloadstatus (TRUE);
  pwnd->getdlgitem (Idc_button1)->enablewindow (TRUE);
 }
 return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.