Internet File Downloading Function

Are you looking for a simple method to download a file from the net? One with error checking and all? Well, this is by far one of the most well written function for just such an occasion I've ever written.

Yes, it does lack File Size detection among other things, but that can be easily added . . .


CString GetFile(const char *url, const char *filename)
{
     #define HTTPBUFLEN    512 // Size of HTTP Buffer...
     char httpbuff[HTTPBUFLEN];
     TCHAR   szCause[255];
     CString Cause;
     Cause.Format("YES");
 
     TRY
     {
          CInternetSession mysession;
          CStdioFile *remotefile = mysession.OpenURL(url,1,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_RELOAD);
 
          CFile myfile(filename, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
          int numbytes;
 
          while (numbytes = remotefile->Read(httpbuff, HTTPBUFLEN))
               myfile.Write(httpbuff, numbytes);
     }
 
     CATCH_ALL(error)
     {
          error->GetErrorMessage(szCause,254,NULL);
          Cause.Format("%s",szCause);
     }
     END_CATCH_ALL;
 
     return (Cause);
}
This will return a "YES" if all goes right, and if not, you can simply MessageBox(); the return value, since MFC's errors are usually pretty self-explanatory :) Also, I recommend making HTTPBUFLEN and httpbuff into global values . . . That way they can be changed on the fly . . . You could even make HTTPBUFLEN into a function defined value.

In any case this small function should deliver quite an easy / cost effective solution for downloading files . . .



Comments

  • Where is the complete source code

    Posted by keenlearner on 04/21/2007 05:57am

    May I know where is the complete source code pleasee ?

    Reply
  • Very good

    Posted by Legacy on 02/26/2004 12:00am

    Originally posted by: Baek Seung Ki

    code is short but very useful.
    thank you.

    Reply
  • rtsp download

    Posted by Legacy on 03/16/2003 12:00am

    Originally posted by: zhanglixin

    http://www.ietf.org/rfc/rfc2326.txt

    here you can find theinformaiton of rtsp controls

    let us share it..

    Reply
  • How can i download binary file like image?

    Posted by Legacy on 03/13/2002 12:00am

    Originally posted by: monir

    It returns String but if the file in the internet was in binary mode, what will be happen then? plese give me some solution for image file like jpg,gif etc.

    Thanks.
    -Monir
    Monir@tigerit.com

    Reply
  • How can I check first if the file exists

    Posted by Legacy on 02/27/2002 12:00am

    Originally posted by: Bob

    The problem with this function is that even if the file doesnt exist, it still creates a file on my computer with some data in it.

    Is there anyway to know if the file exists before dowloading?

    thanks

    Reply
  • fixed memory leak with GetFile() function

    Posted by Legacy on 01/20/2002 12:00am

    Originally posted by: Jay Beckert

    Very good easy code that does the job it needs to do.
    Just a simple fix though. Add this after the file is
    wrote to disk.
    remotefile->Close();
    mysession.Close();

    So the whole code looks as follows:

    CString GetFile(const char *url, const char *filename)
    {
    #define HTTPBUFLEN 512 // Size of HTTP Buffer...
    char httpbuff[HTTPBUFLEN];
    TCHAR szCause[255];
    CString Cause;
    Cause.Format("YES");

    TRY
    {
    CInternetSession mysession;
    CStdioFile *remotefile = mysession.OpenURL(url,1,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_RELOAD);

    CFile myfile(filename, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
    int numbytes;

    while (numbytes = remotefile->Read(httpbuff, HTTPBUFLEN))
    myfile.Write(httpbuff, numbytes);
    remotefile->Close();
    mysession.Close();
    }

    CATCH_ALL(error)
    {
    error->GetErrorMessage(szCause,254,NULL);
    Cause.Format("%s",szCause);
    }
    END_CATCH_ALL;

    return (Cause);
    }

    Reply
  • What about downloading from security servers?

    Posted by Legacy on 10/17/2001 12:00am

    Originally posted by: Anthony

    How can I download file(s) from servers which need a password? Where should be login and password data implemented?
    
    Thank you.

    Reply
  • Thanks!!

    Posted by Legacy on 08/14/2001 12:00am

    Originally posted by: Jethro Kangleon

    Thanks a lot for the code!

    Reply
  • How about under Windows 3.1.

    Posted by Legacy on 07/30/2001 12:00am

    Originally posted by: Ryan Allan

    This works great and all on 32-bit environments. Does anyone have a simple solution for a file download in a 16-bit environment?

    Reply
  • How can i get the status of a file transfer?

    Posted by Legacy on 05/09/2001 12:00am

    Originally posted by: Sasikala.V.B.

    How can i get the status of a file transfer
    (whether it is transfered or not)?

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds