CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 1999
    Posts
    8

    MFC CFtpConnection class and download status.

    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


  2. #2
    Guest

    Re: MFC CFtpConnection class and download status.

    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:ownloadWebPage( 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
    [email protected]






  3. #3
    Join Date
    Apr 1999
    Posts
    10

    Re: MFC CFtpConnection class and download status.

    To get the size of the file use:

    iFile->GetLength()
    or
    myFtpFileFind.GetLength()

    Good Luck!

    Oscar Rivera


  4. #4
    Join Date
    Apr 1999
    Posts
    8

    Re: MFC CFtpConnection class and download status.

    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


  5. #5
    Join Date
    Apr 1999
    Posts
    10

    Re: MFC CFtpConnection class and download status.

    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



  6. #6
    Guest

    Re: MFC CFtpConnection class and download status.

    Do you know how to write FTP program without using WinInet?


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured