zoukankan      html  css  js  c++  java
  • 下载文件并显示进度条

    下载文件并显示进度条 - Fly - 从C开始

    UINT DownloadFile(LPVOID pParam)
    {
     CWnd*   pwnd = AfxGetMainWnd();
     CProgressCtrl* m_Prog = (CProgressCtrl*)pwnd->GetDlgItem(IDC_PROGRESS1);
     CButton*  bStart = (CButton*)pwnd->GetDlgItem(IDB_BTN_START);
     
     char    filebuf[512];
     CInternetSession netSession;
     CStdioFile   *fTargFile;
     int     outfs;
     CString    szFile,FileSize,KBin,KBsec,NewName,Perc;
     
     try
     {
      pwnd->GetDlgItemText(IDC_EDIT1,szFile);
      pwnd->SetDlgItemText(IDC_STAT,"正在校验下载地址...");
      fTargFile = netSession.OpenURL(szFile,1,INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD);
      nDownloaded = 1;

      COleDateTime dlStart = COleDateTime::GetCurrentTime();
      int filesize = fTargFile->SeekToEnd();
      fTargFile->SeekToBegin();
      outfs = filesize / 1024;  // 计算文件大小(千字节)
      FileSize.Format("%d",outfs); // 以KB为单位格式文件大小
      
      // 在当前目录创建新的目标文件
      CFile fDestFile(fTargFile->GetFileName(), CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
      int byteswrite;  // 写入文件的字节数
      int pos = 0;  // 当前进度条的位置
      int nperc,kbrecv; // 进度条的百分比,获取到的数据大小(Kbs为单位)
      double secs,kbsec; // 记录秒数, 速度(KB/秒)
     
      pwnd->SetDlgItemText(IDC_STAT,"正在下载...");
      m_Prog->SetRange32(0,filesize);
      
      while (byteswrite = fTargFile->Read(filebuf, 512)) // 读取文件
      {
       if(nTerminate == 1)      // 如果点击取消下载
       {
        fDestFile.Close();     // 关闭我们的目标文件
        fTargFile->Close();     // 关闭远程文件
        delete fTargFile;     // 删除CStdioFile对象,以防止泄漏
        pwnd->SetDlgItemText(IDC_STAT,"下载时已被用户取消!"); // Set satus bar text
        AfxEndThread(0);     // 结束下载线程
       }

       // 根据开始时间与当前时间比较,获取秒数
       COleDateTimeSpan dlElapsed = COleDateTime::GetCurrentTime() - dlStart;
       secs = dlElapsed.GetTotalSeconds();
       pos = pos + byteswrite;     // 设置新的进度条位置
       fDestFile.Write(filebuf, byteswrite); // 将实际数据写入文件
       m_Prog->SetPos(pos);
       
       nperc = pos * 100 / filesize;   // 进度百分比
       kbrecv = pos / 1024;     // 获取收到的数据
       kbsec = kbrecv / secs;     // 获取每秒下载多少(KB)

       Perc.Format("%d",nperc);    // 格式化进度百分比
       KBin.Format("%d",kbrecv);    // 格式化已下载数据大小(KB)
       KBsec.Format("%d",(int)kbsec);   // 格式化下载速度(KB/秒)

       pwnd->SetDlgItemText(IDC_EDIT_FILESIZE,FileSize + "KB");// 远程文件大小
       pwnd->SetDlgItemText(IDC_EDIT_SIZEOK,KBin + "KB");  // 已下载大小
       pwnd->SetDlgItemText(IDC_EDIT2,KBsec + "KB/秒");  // 下载速度
       pwnd->SetDlgItemText(IDC_EDIT4,Perc + "%");    // 进度百分比
      }
      // 下载完成,关闭文件
      fDestFile.Close();
     }
     catch(CInternetException *IE)
     {
      CString strerror;
      TCHAR error[255];

      IE->GetErrorMessage(error,255); // 获取错误消息
      strerror = error;

      pwnd->SetDlgItemText(IDC_STAT,strerror);
      pwnd->SetDlgItemText(IDB_BTN_STOP,"Exit");
      nDownloaded = 0;
      delete fTargFile;
      IE->Delete();     // 删除异常对象,以防止泄漏
     }
     // 恢复默认
     pwnd->SetDlgItemText(IDC_EDIT2,"Kb/秒");
     pwnd->SetDlgItemText(IDC_EDIT3,"Loading...");
     pwnd->SetDlgItemText(IDC_EDIT4,"0%");
     
     delete fTargFile;
     if(nDownloaded == 1)
     {
      pwnd->SetDlgItemText(IDC_STAT,"下载完成!");
      bStart->EnableWindow(TRUE);
     }
     return 0;
    }

  • 相关阅读:
    Select2插件的隐藏、设置宽度
    远程登陆Linux服务器
    ECMAScript typeof用法
    Select2异步搜索数据
    ECMAScript 引用类型
    LeetCode 任务调度器-Python3<八>
    sort、sorted高级排序-Python3.7 And 算法<七>
    LeetCode算法笔记目录
    数据结构-Python3.7<三>
    【夯实PHP基础】php开发时遇到白页的调试方法
  • 原文地址:https://www.cnblogs.com/lidabo/p/3572964.html
Copyright © 2011-2022 走看看