MFC+FTP上传文件实例

建立单文档时:

一步一步来的时候,需要选中采用window socket选项

建立FTP服务器:

 HOME FTP SERVER服务器建立的

登录:

void CFtpDlg::OnBnClickedButtonLogin()
{
    // TODO: 在此添加控件通知处理程序代码
    try
    {
        // Request a connection to ftp.microsoft.com. Default
        // parameters mean that we'll try with username = ANONYMOUS`
        // and password set to the machine name @ domain name
        UpdateData();
        if (m_csAddress.IsEmpty())
        {
            MessageBox("服务器地址不能为空");
            GetDlgItem(IDC_EDIT_ADDRESS)->SetFocus();
            return;
        }
        if (m_csUserName.IsEmpty())
        {
            MessageBox("用户名不能为空");
            GetDlgItem(IDC_EDIT_USER_NAME)->SetFocus();
            return;
        }
//         CString strTemp;
//         strTemp.Format("%s,%s,%s",m_csAddress,m_csUserName,m_csPwd);
//         MessageBox(strTemp);

        //正式建立连接
        pConnect = sess.GetFtpConnection(m_csAddress,m_csUserName,m_csPwd);

        //pConnect = sess.GetFtpConnection("localhost","wang","851188");
    }
    catch (CInternetException* pEx)
    {
        TCHAR sz[1024];
        pEx->GetErrorMessage(sz, 1024);

        CString errstr;
        errstr.Format("%s",sz);
        //printf("ERROR!  %s\n", sz);
        MessageBox(errstr);
        pEx->Delete();
    }



    if (pConnect!=NULL)
    {
        GetDlgItem(IDC_STATIC_TIP_INFO)->SetWindowText("登录成功!");
        GetDlgItem(IDC_EDIT_USER_NAME)->SetWindowText("");
        GetDlgItem(IDC_EDIT_PASSWORD)->SetWindowText("");
    }
    else
    {
        GetDlgItem(IDC_STATIC_TIP_INFO)->SetWindowText("没有登录成功!");
    }
}

上传文件 :

void CFtpDlg::OnBnClickedButtonUpload()
{
    BOOL isUpload=FALSE;
    UpdateData();
    CString str;
    //截取文件名
    int i=m_filePath.ReverseFind('\\');
    str+=m_filePath.Mid(i+1);


    if (pConnect!=NULL)
    {
        isUpload=pConnect->PutFile(m_filePath,str);
        //m_mfcEditBrowseCtrl.GetStyle();
    }

    if (isUpload)
    {
        MessageBox("上传文件成功");
    }
    else
    {
        MessageBox("上传文件失败");
    }


}


你可能感兴趣的:(MFC+FTP上传文件实例)