Click here to Skip to main content
15,883,705 members
Articles / Desktop Programming / MFC
Article

FTP File Download Made easy

Rate me:
Please Sign up or sign in to vote.
3.63/5 (8 votes)
16 Jul 20012 min read 145.4K   40   25
A simple function for downloading files from ftp sites using wininet classes

Introduction

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 http://www.codeguru.com/internet/FileUpload.shtml

The Function

This function uses the Wininet interface classes: CinternetSession, CftpConnection, and CftpFileFind. You pass in the FTP server (e.g ftp.microsoft.com), 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;
    sCause.Format("");  
 
    try
    {
        //create the internet session 
        CInternetSession mysession;

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

          CFtpFileFind finder(pFtpConn);

          BOOL bWorking = finder.FindFile(remotefile);

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

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

        
    }
 
    catch(CException* error)
    {
        error->GetErrorMessage(szCause,254,NULL);
        sCause.Format("%s",szCause);
    }
    
 
    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.

Usage

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

..

Finally

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.

Cheers!
Ali

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGet the list of files available on FTP Pin
ledallam10-Apr-06 2:42
ledallam10-Apr-06 2:42 
GeneralQuestion on Embedded visual C++ Pin
dick52412-Jan-06 1:16
dick52412-Jan-06 1:16 
GeneralExpanding Tree Pin
vic_sk26-May-05 16:46
vic_sk26-May-05 16:46 
Questionhow to test Pin
doguhan26-Feb-05 12:05
doguhan26-Feb-05 12:05 
GeneralAn Interesting Problem Pin
Kuki Pei20-Nov-04 7:53
Kuki Pei20-Nov-04 7:53 
GeneralGetFile() returns 5 (Access is denied) Pin
anderslundsgard3-Sep-04 1:54
anderslundsgard3-Sep-04 1:54 
GeneralRe: GetFile() returns 5 (Access is denied) Pin
Anil K P26-Oct-07 22:26
Anil K P26-Oct-07 22:26 
GeneralVC++ 6 question Pin
inegoita10-Jun-04 11:15
inegoita10-Jun-04 11:15 
Questionhow to integrate into a dll? assertion fail Pin
Locura66621-May-04 14:37
Locura66621-May-04 14:37 
AnswerRe: how to integrate into a dll? assertion fail Pin
bibi_40008-Feb-05 6:09
bibi_40008-Feb-05 6:09 
GeneralRe: how to integrate into a dll? assertion fail Pin
Anonymous10-Oct-05 9:28
Anonymous10-Oct-05 9:28 
GeneralRe: how to integrate into a dll? assertion fail Pin
huangwc198416-Oct-05 17:29
huangwc198416-Oct-05 17:29 
GeneralIntegration Pin
steve2222222214-Jan-03 7:28
steve2222222214-Jan-03 7:28 
GeneralUseful Pin
Anonymous15-Jul-02 16:39
Anonymous15-Jul-02 16:39 
Generalresuming Pin
20-May-02 23:51
suss20-May-02 23:51 
GeneralRe: resuming Pin
Dean Michaud21-May-02 6:58
Dean Michaud21-May-02 6:58 
GeneralRe: resuming Pin
Andrew Medvedev7-Jun-02 3:15
Andrew Medvedev7-Jun-02 3:15 
GeneralRe: resuming Pin
turbos1016-Jul-03 16:09
turbos1016-Jul-03 16:09 
GeneralVC 4 Pin
Jeff Roberts30-Jul-01 5:43
Jeff Roberts30-Jul-01 5:43 
GeneralRe: VC 4 Pin
Carlos Antollini30-Jul-01 5:50
Carlos Antollini30-Jul-01 5:50 
GeneralUpload Pin
18-Jul-01 4:06
suss18-Jul-01 4:06 
GeneralRe: Upload Pin
Ali Issa18-Jul-01 4:35
Ali Issa18-Jul-01 4:35 
GeneralRe: Upload Pin
25-Oct-01 18:57
suss25-Oct-01 18:57 
Hi,
Great thanks to You!
Still I'm trying upload....

Smile | :)

Br Jakke
GeneralRe: Upload Pin
Andrew Medvedev7-Jun-02 2:50
Andrew Medvedev7-Jun-02 2:50 
GeneralReturning error messages Pin
Andrew Peace17-Jul-01 10:17
Andrew Peace17-Jul-01 10: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.