使用MFC WinInet进行FTP中文件的简单上传和下载功能

  建立基于对话框的MFC应用程序CMfcFtpWinInetDlg:

  1、首先Dlg类中包含头文件 #include "afxinet.h"

  2、添加成员变量:

 C++ Code 
1
2
3
 
  private:
    CFtpConnection*     m_pFtpConnection;
    CInternetSession    m_Session;
 

  3、在OnInitDialog中加入Ftp连接代码:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
// TODO: Add extra initialization here
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(_T(
"localhost" ), _T( "Manager" ), _T( "kingview" ), INTERNET_INVALID_PORT_NUMBER);
}
catch (CInternetException *pEx)
{
    pEx->ReportError(MB_ICONEXCLAMATION);
    m_pFtpConnection = 
NULL ;
    pEx->Delete();
}

  4、在OK和Cancel按钮中加入关闭Ftp连接代码:

 C++ Code 
1
2
3
4
5
6
 
// TODO: Add your control notification handler code here
m_Session.Close();
m_pFtpConnection->Close();

if (m_pFtpConnection !=  NULL )
    
delete  m_pFtpConnection;

   5、添加Upload以及Download按钮代码:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 
void  CMfcFtpWinInetDlg::OnBnClickedBtnUpload()
{
    
// TODO: Add your control notification handler code here
    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(_T( "F:\\hot.gif" ))==TRUE)
    {
        Finder.FindNextFile();
        strFileName = Finder.GetFileName();
        Finder.Close();
    }

    BOOL bUploaded = m_pFtpConnection->PutFile( _T(
"F:\\hot.gif" ),
                                                strFileName,
                                                FTP_TRANSFER_TYPE_BINARY,
                                                
1 );

    AfxMessageBox(_T(
"Uploaded Successfully" ));
}

void  CMfcFtpWinInetDlg::OnBnClickedBtnDownload()
{
    
// TODO: Add your control notification handler code here
    BOOL bDownloaded = m_pFtpConnection->GetFile(   _T( "hot.gif" ),
                                _T(
"D:\\hot.gif" ),
                                TRUE,
                                FILE_ATTRIBUTE_NORMAL,
                                FTP_TRANSFER_TYPE_BINARY,
                                
1 );
    AfxMessageBox(_T(
"Downloaded Successfully" ));
}

   API参数说明,请参考MSDN。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值