|
-
February 12th, 2005, 03:49 AM
#7
Re: File Transfer
Code:
void CopyFromFTPServer()
{
int iRC = 0;
CFile LocalFile;
CInternetFile *pInternetFile = 0;
CFtpConnection *pFTPConnection = 0;
CInternetSession *pInternetSession = 0;
pInternetSession = new CInternetSession();
if(pInternetSession)
{
try
{
pFTPConnection = pInternetSession->GetFtpConnection(_T("ftp.server.com"), _T("username"), _T("password"));
pInternetFile = pFTPConnection->OpenFile(_T("test.txt"));
LocalFile.Open(_T("c:\\test.txt"), CFile::modeCreate | CFile::modeWrite);
// Read data
TCHAR szBuffer[50] = "";
while((iRC = pInternetFile->Read(static_cast<void *>(szBuffer), sizeof(szBuffer))) != 0)
{
// Write to file
LocalFile.Write(static_cast<const void *>(szBuffer), iRC);
// Clear buffer
memset(szBuffer, 0, sizeof(szBuffer));
}
}
catch(CInternetException* pEx)
{
TCHAR szErrorMessage[1024] = "";
pEx->GetErrorMessage(szErrorMessage, sizeof(szErrorMessage) - 1);
AfxMessageBox(szErrorMessage, "Error");
pEx->Delete();
}
}
// If the connection is open, close it
if(pFTPConnection)
pFTPConnection->Close();
delete pInternetSession;
}
void CopyToFTPServer()
{
int iRC = 0;
CFile LocalFile;
CInternetFile *pInternetFile = 0;
CFtpConnection *pFTPConnection = 0;
CInternetSession *pInternetSession = 0;
pInternetSession = new CInternetSession();
if(pInternetSession)
{
try
{
pFTPConnection = pInternetSession->GetFtpConnection(_T("ftp.server.com"), _T("username"), _T("password"));
pInternetFile = pFTPConnection->OpenFile(_T("test.txt"), GENERIC_WRITE);
LocalFile.Open(_T("c:\\test.txt"), CFile::modeRead);
// Write data
TCHAR szBuffer[50] = "";
while((iRC = LocalFile.Read(static_cast<void *>(szBuffer), sizeof(szBuffer))) != 0)
{
// Write to file
pInternetFile->Write(static_cast<const void *>(szBuffer), iRC) ;
// Clear buffer
memset(szBuffer, 0, sizeof(szBuffer));
}
}
catch(CInternetException* pEx)
{
TCHAR szErrorMessage[1024] = "";
pEx->GetErrorMessage(szErrorMessage, sizeof(szErrorMessage) - 1);
AfxMessageBox(szErrorMessage, "Error");
pEx->Delete();
}
}
// If the connection is open, close it
if(pFTPConnection)
pFTPConnection->Close();
delete pInternetSession;
}
Last edited by Andreas Masur; February 12th, 2005 at 05:03 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|