Topic : FTP Uploading
Author : Ashman
Page : 1

Okay, now this article is for all those people out there who are probably like me, trying to write an application that will connect to their server so that they can upload files.

Okay, this is dialog based because dialog programming is just the way to go....it looks soooo cool :) anyway.

You have to call the afxinet class crap first so at the top of yourprojectDlg.cpp you add the line,

#include "ifxinet.h" //Needed for internet stuff heh.

Now, you have to find the file you want to upload. So create an edit box and call it ummm....hmmm, lets call it IDC_FILELOCATION and create a member variable, m_filelocation. Now create a browse button and call it IDC_BROWSE and create OnBrowse(); Add this code to the OnBrowse function:

CFileDialog cfd(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "File type description here|*.*|", NULL);

if(cfd.DoModal() != IDOK)
return;

m_filelocation = cfd.GetFileName(); //adds the file name to the edit box.
//Generally you would use GetPathName(); but because we are uploading, it would upload the whole path name instead of just the file name.

UpdateData(FALSE); //Blah :)


Okay, now create your upload button and make the member function for the button, OnUpload();.

This code connects you to your server. It goes into OnUpload();

UpdateData(TRUE);

BeginWaitCursor();
CInternetSession cint;
CFtpConnection *ftpConn = NULL;

ftpConn = cint.GetFtpConnection("Server IP","Username","password");
if(ftpConn == NULL)
{
EndWaitCursor();
MessageBox("Unable to connect to the server");
return;
}


Okay now we need to navigate to the folder we want to upload to.

//the directory where uploaded files will be stored
if(ftpConn->SetCurrentDirectory("/yourfolder") == 0)
{
EndWaitCursor();
MessageBox("Unable to navigate to directory");
return;
}



Now to upload your file:

if(ftpConn->PutFile(m_maplocation, m_maplocation, FTP_TRANSFER_TYPE_BINARY, 10) == 0)
{
TRACE("Error uploading file: %dn", GetLastError());
MessageBox("Unable to upload file");
}
else
MessageBox("File successfully uploaded");


Cool almost done, now to close the connection:

ftpConn->Close();
cint.Close();
delete ftpConn;

EndWaitCursor();


SIMMMPLE! I hope you enjoy this!
Ashman
www.settlers.net
The Settlers Network!

Page : 1