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

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds