<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    下載文件并顯示進度條

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接 下載文件并顯示進度條 - 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;
    }

    RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全