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

  • Relying on outside companies to manage your network and server environments for your business and applications to meet the needs and demands of your users can be stressful. This is especially true as many Managed Hosting organizations fail to meet their service level agreements. Read this Forrester total economic impact report and learn what makes INetU different and how they exceed their customers' managed hosting expectations.

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

Most Popular Programming Stories

More for Developers

RSS Feeds