Internet File Downloading Function | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Dec 9, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More


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 . . .

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.