65.9K
CodeProject is changing. Read more.
Home

Download files from internet

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.71/5 (12 votes)

Dec 3, 2004

viewsIcon

44376

downloadIcon

2041

Download the files from internet

Introduction

This application used to download the file from internet by giving the path.
#include <afxinet.h> 
CInternetSession netSession; 
CInternetSession having the member function OpenURL() to open the URL.
    CStdioFile *fTargetFile;    
    fTargetFile=netSession.OpenURL(str,1,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_RELOAD);
This application displays the download rate and show how much percent downloaded in progress bar.
    int filesize=fTargetFile->SeekToEnd();
         int outfs;
    outfs=filesize/1024;
CProgressCtrl *prg=(CProgressCtrl*)GetDlgItem(IDC_PRG1);
    COleDateTime StartTime=COleDateTime::GetCurrentTime();
    prg->SetRange32(0,filesize);
        int bytesread=0,pos=0;
    char strbuf[512];
       CString szDestPath;
    GetDlgItemText(IDC_EDIT2,szDestPath);
    szDestPath+="\\";
    szDestPath+=fTargetFile->GetFileName();
    CFile fDestFile(szDestPath,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
    while(bytesread=fTargetFile->Read(strbuf,512))
    {
        pos+=bytesread;
        prg->SetPos(pos);            
        
        float percent=(float)pos/(float)filesize;
        int p=percent*100;
        fDestFile.Write(strbuf,bytesread);
        COleDateTimeSpan dlTimeElapsed=COleDateTime::GetCurrentTime()-StartTime;
        secs=dlTimeElapsed.GetTotalSeconds();

        int kb=pos/1024;
        kbpsecs=kb/secs;
        CString Kbps,szPercent;
        Kbps.Format("%0.1f KB/Sec",kbpsecs);
        SetDlgItemText(IDC_STATIC1,Kbps);
        
        szPercent.Format("%d %%",p);
        SetDlgItemText(IDC_STATIC4,szPercent);
        Sleep(100);
    }
This code used to download the files from internet by giving the path.