Click here to Skip to main content
15,881,938 members
Articles / Desktop Programming / MFC
Article

Basics of uploading files to and downloading from an ftp server

Rate me:
Please Sign up or sign in to vote.
4.12/5 (20 votes)
30 Jul 20022 min read 162.7K   64   23
This tutorial helps you to upload and download files from an FTP server.

Introduction

This article explains and demonstrates the usage of the File Transfer Protocol (FTP) to upload your files to a server on the internet and to download files from a FTP server. This requires you to have the username and password to the ftp server where you are going to upload files or from where you are going to download files.

Usage

FTP has many uses among which I can give you a few beginners examples. You can use it to store data on the server which can be accessed by your fellow programmers who might be working in different locations. The way I used it was for  the purpose of displaying a PowerPoint slideshow in one place and simultaneously broadcasting it to many clients. The software captured the screenshots and saved them as a .jpg file on the ftp server. My client software used to continuously access that server checking for new files and display them whenever a change is made. The client software was basically a JPEG viewer software. You can cook up a lot more usages depending on your requirements.

Implementation

To use ftp, you need to add the afxinet.h header file.

  • Add the following statement in your dialog's header file or if its a doc/view project, then add it to your document class's header file.
  • #include "afxinet.h"
  • In the header file ( dialog's or document class's ), add the following Member Variables
  • CFtpConnection *m_pFtpConnection;
    CInternetSession m_Session;
  • In your application's initialization ( OnInitDialog or InitInstance functions), add the following lines.
  • m_pFtpConnection = NULL;
    
    try
    {
        // Here usr is the username, pwd is the password 
        // and ftpsite.com is the name of the ftp site which
        // you want to connect to.
    
        m_pFtpConnection = m_Session.GetFtpConnection("ftpSite.com",
            "usr","pwd",INTERNET_INVALID_PORT_NUMBER);
    }
    catch(CInternetException *pEx)
    {
        pEx->ReportError(MB_ICONEXCLAMATION);
        m_pFtpConnection = NULL;
        pEx->Delete();
    }
    return TRUE;
  • To upload a file, add the following lines of code :-
  • CFileFind Finder;
    CString strFileName;
    
    // Here c:\\Myfile.bmp is the name of the file that you want 
    // to upload. It neednt necessarily be a bitmap file. You 
    // can upload any file that you want to.
    // The CString strFileName is used so that the same name 
    // is uploaded to the ftp server.
    // After uploading, the file in the ftp server will have 
    // the same name as your local file.
    // You can also rename it to anything
    
    if(Finder.FindFile("C:\\Myfile.bmp")==TRUE)
    {
        Finder.FindNextFile();
        strFileName = Finder.GetFileName();
        Finder.Close();
    }
    
    BOOL bUploaded = m_pFtpConnection->PutFile("C:\\Myfile.bmp",
                        strFileName,FTP_TRANSFER_TYPE_BINARY,1);
    
    AfxMessageBox("Uploaded Successfully");
  • To download a file from a ftp site, you can use the following code. Here the first parameter is the file in the ftp server. The 2nd parameter is the location where you want to store it in your hard disk.
  • m_pFtpConnection->GetFile("File.ext","C:\\File.ext",
        TRUE,FILE_ATTRIBUTE_NORMAL,FTP_TRANSFER_TYPE_BINARY,1);
  • To close the connection
  • m_Session.Close();
    m_pFtpConnection->Close();
    
    if(m_pFtpConnection!=NULL)
        delete m_pFtpConnection;

Revision History

  • 31 Jul 2002 - Initial Revision [Basic Revision]

Conclusion

FTP is easy to use and has a lots of uses. Think of your own implementations and various features that can be added to the supplied code. That's it. All luck and have a great time.

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

Comments and Discussions

 
QuestionCan not upload Chinese file's name to ftp server! Pin
Member 1037175115-May-14 21:29
Member 1037175115-May-14 21:29 
GeneralUploading To A Folder On The FTP Pin
geoffism24-Mar-05 13:49
geoffism24-Mar-05 13:49 
GeneralWin XP if service pack 2 problem Pin
Rishabh Agrawal17-Feb-05 0:58
Rishabh Agrawal17-Feb-05 0:58 
GeneralRe: Win XP if service pack 2 problem Pin
Anonymous16-Mar-05 22:21
Anonymous16-Mar-05 22:21 
Questiondid someone get this working? Pin
muurimc10-Nov-03 4:00
muurimc10-Nov-03 4:00 
AnswerRe: did someone get this working? Pin
riki_risnandar15-Dec-04 9:09
riki_risnandar15-Dec-04 9:09 
Questionhow to use FtpGetFileStatus() here? Pin
Member 4947571-Oct-03 17:30
Member 4947571-Oct-03 17:30 
GeneralFtp Connection Not working Pin
girishnadig18-Sep-03 6:34
girishnadig18-Sep-03 6:34 
GeneralRe: Ftp Connection Not working Pin
code_junkie16-Nov-07 10:21
code_junkie16-Nov-07 10:21 
Questionhow to get the source code? Pin
Anonymous18-May-03 15:24
Anonymous18-May-03 15:24 
AnswerRe: how to get the source code? Pin
Anonymous24-Jun-03 4:49
Anonymous24-Jun-03 4:49 
GeneralProblem with uploading Pin
Anonymous14-Oct-02 9:02
Anonymous14-Oct-02 9:02 
I am using the uploading part of this code and it fails every time. What might I be doing wrong???
GeneralRe: Problem with uploading Pin
huangwc19848-Oct-04 22:34
huangwc19848-Oct-04 22:34 
QuestionHow to Copy/Move Temporary Internet Files? Pin
Stuard20-Sep-02 9:29
Stuard20-Sep-02 9:29 
AnswerRe: No Pin
Tim Smith20-Sep-02 9:42
Tim Smith20-Sep-02 9:42 
AnswerRe: How to Copy/Move Temporary Internet Files? Pin
philippe dykmans13-Oct-02 23:52
philippe dykmans13-Oct-02 23:52 
GeneralGet all remote files Pin
Anonymous31-Jul-02 2:32
Anonymous31-Jul-02 2:32 
QuestionHow to upload with resumes? Pin
Paresh Solanki31-Jul-02 0:36
Paresh Solanki31-Jul-02 0:36 
QuestionHow to Upload Without FTP Pin
Abhishek Narula1-Jul-02 18:45
Abhishek Narula1-Jul-02 18:45 
AnswerRe: How to Upload Without FTP Pin
VGirish2-Jul-02 0:24
VGirish2-Jul-02 0:24 
GeneralRe: How to Upload Without FTP Pin
Abhishek Narula2-Jul-02 1:28
Abhishek Narula2-Jul-02 1:28 
GeneralPlease fix this up Pin
Jim Crafton29-Jun-02 10:00
Jim Crafton29-Jun-02 10:00 
GeneralRe: Please fix this up Pin
Brian Delahunty30-Jun-02 9:00
Brian Delahunty30-Jun-02 9:00 

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.