Click here to Skip to main content
15,899,679 members
Articles / Desktop Programming / MFC
Article

Download files from internet

Rate me:
Please Sign up or sign in to vote.
1.71/5 (18 votes)
2 Dec 2004 44.2K   2K   18   6
Download the files from internet

Introduction

This application used to download the file from internet by giving the path.
#include <afxinet.h> <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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
I am working at Virinchi Technologies as programmer in vc++,COM & symbian c++. My hobbies are painting, and play chess and carams.

Comments and Discussions

 
QuestionNice, simple and it works Pin
rbrunton16-Aug-17 6:02
rbrunton16-Aug-17 6:02 
QuestionI have used ur liabrary to download zip file . Pin
Member 1120814210-Nov-14 2:01
Member 1120814210-Nov-14 2:01 
Questiononly 5 kbps ?why ? Pin
mircea1313139-Mar-07 4:35
mircea1313139-Mar-07 4:35 
AnswerRe: only 5 kbps ?why ? [modified] Pin
ABates8525-Oct-07 23:11
ABates8525-Oct-07 23:11 
GeneralError in proxy Pin
asfur4-Jan-07 3:33
asfur4-Jan-07 3:33 
Olá this giving error when uses in proxy, as to decide?

Tks.Confused | :confused:
Generalstatus of connect Pin
Kutti Ra30-Sep-05 16:21
Kutti Ra30-Sep-05 16:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.