Click here to Skip to main content
Click here to Skip to main content

FTP File Download Made easy

By , 16 Jul 2001
Rate this:
Please Sign up or sign in to vote.
<!-- Add the rest of your HTML here -->

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

About the Author

Ali Issa
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralGet the list of files available on FTP Pinmemberledallam10-Apr-06 2:42 
GeneralQuestion on Embedded visual C++ Pinmemberdick52412-Jan-06 1:16 
GeneralExpanding Tree Pinmembervic_sk26-May-05 16:46 
Questionhow to test Pinmemberdoguhan26-Feb-05 12:05 
GeneralAn Interesting Problem PinmemberKuki Pei20-Nov-04 7:53 
GeneralGetFile() returns 5 (Access is denied) Pinmemberd00_ape3-Sep-04 1:54 
GeneralRe: GetFile() returns 5 (Access is denied) PinmemberAnil K P26-Oct-07 22:26 
GeneralVC++ 6 question Pinmemberinegoita10-Jun-04 11:15 
Questionhow to integrate into a dll? assertion fail PinmemberLocura66621-May-04 14:37 
AnswerRe: how to integrate into a dll? assertion fail Pinmemberbibi_40008-Feb-05 6:09 
GeneralRe: how to integrate into a dll? assertion fail PinsussAnonymous10-Oct-05 9:28 
GeneralRe: how to integrate into a dll? assertion fail Pinmemberhuangwc198416-Oct-05 17:29 
GeneralIntegration PinsussSteve2222222214-Jan-03 7:28 
GeneralUseful PinsussAnonymous15-Jul-02 16:39 
Generalresuming PinmemberAnonymous20-May-02 23:51 
GeneralRe: resuming PinmemberDean 'Karnatos' Michaud21-May-02 6:58 
GeneralRe: resuming PinmemberAndrew Medvedev7-Jun-02 3:15 
GeneralRe: resuming PinsussKamil Dziobek16-Jul-03 16:09 
GeneralVC 4 PinmemberJeff Roberts30-Jul-01 5:43 
GeneralRe: VC 4 PinmemberCarlos Antollini30-Jul-01 5:50 
GeneralUpload PinmemberAnonymous18-Jul-01 4:06 
GeneralRe: Upload PinmemberAli Issa18-Jul-01 4:35 
GeneralRe: Upload PinmemberJakke25-Oct-01 18:57 
GeneralRe: Upload PinmemberAndrew Medvedev7-Jun-02 2:50 
GeneralReturning error messages PinmemberAndrew Peace17-Jul-01 10:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.8.140415.2 | Last Updated 17 Jul 2001
Article Copyright 2001 by Ali Issa
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid