Click here to Skip to main content
12,691,789 members (29,019 online)
Click here to Skip to main content
Add your own
alternative version


39 bookmarked

FTP File Download Made easy

, 16 Jul 2001
Rate this:
Please Sign up or sign in to vote.
A simple function for downloading files from ftp sites using wininet classes
<!-- Add the rest of your HTML here -->


I needed a simple function that downloads files from the internet (FTP or HTTP). I searched in codeproject and there was nothing useful for what I wanted. Then I searched codeguru and stumbled on a very simple function that did more or less what I wanted. I had to modify it, a lot actually, but it gave me a starting point. Since I needed to download files from an ftp site I made the function ftp specific. This will work for ftp sites that allow anonymous login, but it should be easy enough to add the user name and password to the function parameters to allow access to restricted FTP sites. Andon M. Coleman wrote the original function. You can find it at the codeguru site, at

The Function

This function uses the Wininet interface classes: CinternetSession, CftpConnection, and CftpFileFind. You pass in the FTP server (e.g, the file name on the server with the path name (e.g “/pub/file.pdf”) and the name you want to save it as (eg. "c:\downloads\file.pdf". You don’t have to be logged on to the internet while you running this code, but you should have proxy authentication if you are connected through a proxy server. This builds with VC++ 6.0, however if you need to include the wininet classes there in "afxinet.h"
CString CMyApp::GetFTPFile(const char *ftpserver, const char *remotefile, 
                      const char *localfile )
    TCHAR   szCause[255];
    CString sCause;
        //create the internet session 
        CInternetSession mysession;

        //get the ftp connection from internet session
        CFtpConnection* pFtpConn = mysession.GetFtpConnection(ftpserver);
          //we have successfully connected
          //try to find the remote file 

          CFtpFileFind finder(pFtpConn);

          BOOL bWorking = finder.FindFile(remotefile);

            // it exist .. so let's get it
            BOOL bGot = pFtpConn->GetFile(remotefile,localfile);
            //something wrong with the file.. inform the user
            sCause.Format("The File specified could not be found. "
                       "Check your spelling and try again!!");

          //could not connect
          sCause.Format("Could not connect to ftp server!! \nPlease "
                     "ensure you have correct address and access rights!");

    catch(CException* error)
    return (sCause);
All you have to do is to copy the function and integrate it with your application, since there isn't much code involved i din't see the point in creating a project for this or even a class! I have made the return value a CString to get the error as well as well as a return value.


The return value indicates success if the return string is empty, otherwise you'll get the error.
CString sRet= GetFTPFile("","/pub/subfolder/file.pdf","file.pdf");



As I said this is supposed to be very simple function for beginners or for someone who just wants a quick solution. Feel free to comment, modify this code and let me know if you have a better idea.



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


About the Author

Ali Issa
Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralGet the list of files available on FTP Pin
ledallam10-Apr-06 3:42
memberledallam10-Apr-06 3:42 
GeneralQuestion on Embedded visual C++ Pin
dick52412-Jan-06 2:16
memberdick52412-Jan-06 2:16 
GeneralExpanding Tree Pin
vic_sk26-May-05 17:46
membervic_sk26-May-05 17:46 
Questionhow to test Pin
doguhan26-Feb-05 13:05
memberdoguhan26-Feb-05 13:05 
GeneralAn Interesting Problem Pin
Kuki Pei20-Nov-04 8:53
memberKuki Pei20-Nov-04 8:53 
GeneralGetFile() returns 5 (Access is denied) Pin
d00_ape3-Sep-04 2:54
memberd00_ape3-Sep-04 2:54 
GeneralRe: GetFile() returns 5 (Access is denied) Pin
Anil K P26-Oct-07 23:26
memberAnil K P26-Oct-07 23:26 
GeneralVC++ 6 question Pin
inegoita10-Jun-04 12:15
memberinegoita10-Jun-04 12:15 
Questionhow to integrate into a dll? assertion fail Pin
Locura66621-May-04 15:37
memberLocura66621-May-04 15:37 
AnswerRe: how to integrate into a dll? assertion fail Pin
bibi_40008-Feb-05 7:09
memberbibi_40008-Feb-05 7:09 
GeneralRe: how to integrate into a dll? assertion fail Pin
Anonymous10-Oct-05 10:28
sussAnonymous10-Oct-05 10:28 
GeneralRe: how to integrate into a dll? assertion fail Pin
huangwc198416-Oct-05 18:29
memberhuangwc198416-Oct-05 18:29 
GeneralIntegration Pin
Steve2222222214-Jan-03 8:28
sussSteve2222222214-Jan-03 8:28 
GeneralUseful Pin
Anonymous15-Jul-02 17:39
sussAnonymous15-Jul-02 17:39 
Generalresuming Pin
Anonymous21-May-02 0:51
memberAnonymous21-May-02 0:51 
GeneralRe: resuming Pin
Dean 'Karnatos' Michaud21-May-02 7:58
memberDean 'Karnatos' Michaud21-May-02 7:58 
GeneralRe: resuming Pin
Andrew Medvedev7-Jun-02 4:15
memberAndrew Medvedev7-Jun-02 4:15 
GeneralRe: resuming Pin
Kamil Dziobek16-Jul-03 17:09
sussKamil Dziobek16-Jul-03 17:09 
GeneralVC 4 Pin
Jeff Roberts30-Jul-01 6:43
memberJeff Roberts30-Jul-01 6:43 
GeneralRe: VC 4 Pin
Carlos Antollini30-Jul-01 6:50
memberCarlos Antollini30-Jul-01 6:50 
GeneralUpload Pin
Anonymous18-Jul-01 5:06
memberAnonymous18-Jul-01 5:06 
GeneralRe: Upload Pin
Ali Issa18-Jul-01 5:35
memberAli Issa18-Jul-01 5:35 
GeneralRe: Upload Pin
Jakke25-Oct-01 19:57
memberJakke25-Oct-01 19:57 
GeneralRe: Upload Pin
Andrew Medvedev7-Jun-02 3:50
memberAndrew Medvedev7-Jun-02 3:50 
GeneralReturning error messages Pin
Andrew Peace17-Jul-01 11:17
memberAndrew Peace17-Jul-01 11:17 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170117.1 | Last Updated 17 Jul 2001
Article Copyright 2001 by Ali Issa
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid