博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Using FTP from your MFC application - Quick Reference
阅读量:4120 次
发布时间:2019-05-25

本文共 2143 字,大约阅读时间需要 7 分钟。

Introduction

 

Tutorialfor using FTP

Description

        Thistutorial helps you to use FTP from your applications. You can

        a) Connect to a FTP server and
        b) Upload or Download filesfrom and to the server.

Implementation

Step 1

        Addthe following statement in your headerfile.

        #include < afxinet.h >

Step 2

        Inyour header file, add the following Member Variables

 
        CFtpConnection*m_pFtpConnection;
        CInternetSession m_Session;

Step 3

        Inyour application's initialization ( OnInitDialog or InitInstance functions),

        add the followinglines.
        m_pFtpConnection =NULL;
        try
        {
          // Here usr is the username, pwd is thepassword and ftpsite.com is the name
          // of the ftp site which you want to connectto.
          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;

Step 4

        Toupload a file, add the following lines of code :-

        CFileFindFinder;
        CString strFileName;
        // Here c:\\Myfile.bmp is thename of the file that you want toupload
        // It neednt necessarily be abitmap file. You can upload any file thatyou
        // want to.
        // The CString strFileName isused so that the same name is uploaded
        // to the ftpserver.
        // After uploading, the file inthe ftp server will have the same name as
        // your local file.You can alsorename it to anything else.
        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("UploadedSuccessfully");

Step 5

        Todownload a file from a ftp site, you can use the following code.

        
        Here the first parameter is thefile in the ftpserver.
        The 2nd parameter is thelocation where you want to store it in your harddisk.
        m_pFtpConnection->GetFile("File.ext","C:\\File.ext",TRUE,
                                  FILE_ATTRIBUTE_NORMAL,FTP_TRANSFER_TYPE_BINARY,1);

Step 6

        Toclose the connection

        
        m_Session.Close();
        m_pFtpConnection->Close();
        if(m_pFtpConnection!=NULL)
          delete m_pFtpConnection;

Conclusion

Thats itfolks. Have a great time FTPing... All Luck.

转载地址:http://qwppi.baihongyu.com/

你可能感兴趣的文章
弱类型、强类型、动态类型、静态类型语言的区别是什么?
查看>>
Struts2技术内幕图书 转载
查看>>
Java异常分类
查看>>
项目中的jackson与json-lib使用比较
查看>>
Jackson Tree Model Example
查看>>
j2ee-验证码
查看>>
日志框架logj的使用
查看>>
js-高德地图规划路线
查看>>
常用js收集
查看>>
mydata97的日期控件
查看>>
如何防止sql注入
查看>>
maven多工程构建与打包
查看>>
springmvc传值
查看>>
Java 集合学习一 HashSet
查看>>
在Eclipse中查看Android源码
查看>>
Android使用webservice客户端实例
查看>>
层在页面中的定位
查看>>
[转]C语言printf
查看>>
C 语言 学习---获取文本框内容及字符串拼接
查看>>
C 语言学习 --设置文本框内容及进制转换
查看>>