![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
Putting the FTP Protocol to WorkFTP stands for File Transfer Protocol, and as you can imagine, there are many file-handling aspects here. This protocol is supported by the CFtpConnection class, whose file-handling functions appear in Table 8.2.
Using these functions, you can perform the standard FTP operations, from uploading and downloading files to creating new directories. Next, we put the FTP protocol to work and download a file from the Microsoft FTP site. Creating the FTP ProgramIn the next example, we use the CFtpConnection class to support a typical FTP actiondownloading a file. In this case, we download a file that Microsoft keeps in its FTP main directory, named dirmap.txt. This file holds an outline of the Microsoft FTP site, and its one that Microsoft always keeps in its FTP directory. Create a new MDI project now named FTP and, as we did in the last example, base the view class on the CEditView class in Step 6 of AppWizard. Next, add one new item to the File menu: Download file. When the user selects this item, we open an FTP connection to dirmap.txt at ftp.microsoft.com and download it, then display it. We start by including the afxinet.h header. // FTPView.cpp : implementation of the CFTPView class // #include stdafx.h #include FTP.h #include FTPDoc.h #include FTPView.h #include <afxinet.h> ⇐ . . . Next, we add code to the Download file menu item function OnFiledownloadfile() to create a new Internet session. void CFTPView::OnFileDownloadfile() { CInternetSession* Session = new CInternetSession(); ⇐ . . . Creating an FTP ConnectionNow we create a CFtpConnection object using the CInternetSession classs GetFtpConnection() function, connecting to ftp.microsoft.com. void CFTPView::OnFileDownloadfile() { CInternetSession* Session = new CInternetSession(); CFtpConnection* FTPConnection = Session->GetFtpConnection(<ftp.microsoft.com>); ⇐ if(FTPConnection == NULL) ⇐ return; ⇐ . . . Were ready to download the dirmap.txt file from Microsoft. Downloading a FileWe download dirmap.txt, making a copy of that file locally, with the CFTP-Connection classs GetFile() function. We pass that function the name of the file to get, the local name we want to give it, and a parameter we set to FALSE to overwrite a local copy of the file if it already exists. void CFTPView::OnFileDownloadfile() { CInternetSession* Session = new CInternetSession(); CFtpConnection* FTPConnection = Session->GetFtpConnection(<ftp.microsoft.com>); if(FTPConnection == NULL) return; FTPConnection->GetFile(dirmap.txt, dirmap.txt, FALSE); ⇐ . . . At this point, then, we have downloaded dirmap.txt onto disk. To display that file, we read it back in. Reading the File in from Disk with CFileTo read a file from disk, we use the MFC CFile class. First, we set up a buffer for that files data. void CFTPView::OnFileDownloadfile() { #define BufferLength 2048 ⇐ CInternetSession* Session = new CInternetSession(); CFtpConnection* FTPConnection = Session->GetFtpConnection(<ftp.microsoft.com>); if(FTPConnection == NULL) return; FTPConnection->GetFile(dirmap.txt, dirmap.txt, FALSE); char FTPData[BufferLength]; ⇐ . . . Next, we open the file dirmap.txt by passing that filename to the CFile constructor and indicating that were opening this file for reading. Next, we read 2K of data in from the file on disk into the buffer using the CFile Read() function. void CFTPView::OnFileDownloadfile() { #define BufferLength 2048 CInternetSession* Session = new CInternetSession(); CFtpConnection* FTPConnection = Session->GetFtpConnection(<ftp.microsoft.com>); if(FTPConnection == NULL) return; FTPConnection->GetFile(dirmap.txt, dirmap.txt, FALSE); char FTPData[BufferLength]; CFile file(dirmap.txt, CFile::modeRead); ⇐ file.Read(FTPData, BufferLength); ⇐ . . .
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |