Click to See Complete Forum and Search --> : MFC CFtpConnection class and download status.
Vijay Kumar C
April 6th, 1999, 07:37 AM
Hi EveryOne,
I need to code a FTP client.
I am using the following classes. CInternetSession, CFtpConnection, CInternetFile.
I need to get the download size status as I need to display the Total file size, Down load rate, Time left etc.
How do I get these figures?
The code Slice I'm working with is.
////////////////////////////////////////
FILE * myfile;
char buf[16];
int count;
CInternetSession mySession("test1");
CFtpConnection * myFtpConnection = mySession.GetFtpConnection("whtorcl","c2032","2032",21,FALSE );
myFtpConnection->SetCurrentDirectory("c2001");
CFtpFileFind myFtpFileFind(myFtpConnection,1);
myFtpFileFind.FindFile("extract.dat");
CInternetFile* iFile = myFtpConnection->OpenFile( "doc.txt",GENERIC_READ, FTP_TRANSFER_TYPE_ASCII,1);
myfile = fopen("download.txt","w");
// Get the file size somehow!! ( ? )
// In the loop here keep track of the size downloaded!! ( ? )
while( (count = (File->Read( (void* ) buf, 16) ))== 16)
fprintf(myfile,"%s",buf);
fclose(myfile);
AfxMessageBox("Down Load complete");
////////////////////////////////////////
Warm Regards,
Vijay
April 6th, 1999, 11:05 AM
Hi,
The following function can be used to download a Web Page from the Internet.
/*
*****************************************************************
** Function Name : DownloadWebPage()
** Parameters : CString sURL
** Return Value : void
** Description : Downloads a web page.
*****************************************************************
*/
void CMutualFundsQualityView::DownloadWebPage( CString sURL)
{
try
{
// Get the Web Page from the Web server into a temp file
CString szFileName = _T("TemporaryDownload.html");
DWORD dwReserved = 0;
::URLDownloadToFile(NULL, // LPUNKNOWN pCaller
szURL, // LPCWSTR szURL
szFileName, // LPCTSTR szFileName
dwReserved, // DWORD dwReserved
NULL // LPBINDSTATUSCALLBACK lpfnCB
);
}
catch( CException * e )
{
TCHAR err[ 500 ];
e->GetErrorMessage( err, 500 );
CString sError = err;
}
}//end DownloadWebPage()
The header files required by this function are given below.
#include <urlmon.h>
#include <direct.h>
You can pass a URL to the above function and you will be able to download an internet file onto a local file by name "TemporaryDownload.html" which i have hardcoded in the function.
The Internet Explorer : SDK documentation for URLDownloadToFile says in the Remarks Section as
URLDownloadToFile calls the caller's IBindStatusCallback interface to provide notifications during the binding process. In particular, URLDownloadToFile calls IBindStatusCallback::OnProgress to report the ongoing status of the download. The caller does not need not implement any of the notification callbacks for the function to succeed, but doing so allows the client to receive notifications, if interested, and also allows downloads to be canceled by returning E_ABORT from the OnProgress call.
I am currently able to provide you with these pointers and with this you should be able to do the rest as per your requirements.
Regards
Mahanthi@rocketmail.com
Oscar Rivera
April 6th, 1999, 11:52 AM
To get the size of the file use:
iFile->GetLength()
or
myFtpFileFind.GetLength()
Good Luck!
Oscar Rivera
Vijay Kumar C
April 6th, 1999, 12:51 PM
Hi Oscar,
Thank you very much for your reply.
[1] The problem with CInternetFile->GetLength() is that it gives the Current Logical length, not the length of the file on the FTP server.
[2] myFtpFileFind.GetLength(), returns 0 always. May be it should be called differently ?
May be there is a base class function some where that might give me just what I want.
Warm Regards,
Vijay
Oscar Rivera
April 6th, 1999, 03:05 PM
Following is some code that I have used in my apps:
// =====================================================
CFtpFileFind FtpFileFind(m_pFtpConnection);
bContinue = pFtpFileFind.FindFile(strFileName); // strFileName may contain wildcards
if (!bContinue)
{
FtpFileFind.Close();
return FALSE;
}
while(bContinue)
{
strTmp = _T("/usr/home/") + m_strLogin;
if (m_pFtpConnection->SetCurrentDirectory(strTmp) == 0)
{
strError = _T("Invalid remote directory: ") + strTmp;
MessageBox(strError);
return FALSE;
}
bContinue = pFtpFileFind.FindNextFile();
if (pFtpFileFind.IsDirectory()) continue;
strFileName = pFtpFileFind.GetFileName();
dwFileLength = pFtpFileFind.GetLength();
}
// =====================================================
Hope this helps!
Regards,
Oscar Rivera
Do you know how to write FTP program without using WinInet?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.