Basics of uploading files to and downloading from an ftp server






4.12/5 (18 votes)
Jun 29, 2002
2 min read

164889
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"
CFtpConnection *m_pFtpConnection; CInternetSession m_Session;
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;
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");
m_pFtpConnection->GetFile("File.ext","C:\\File.ext", TRUE,FILE_ATTRIBUTE_NORMAL,FTP_TRANSFER_TYPE_BINARY,1);
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.