Internet File Downloading Function
Posted
by Andon M. Coleman
on December 9th, 1999
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:57amMay I know where is the complete source code pleasee ?
ReplyVery good
Posted by Legacy on 02/26/2004 12:00amOriginally posted by: Baek Seung Ki
code is short but very useful.
Replythank you.
rtsp download
Posted by Legacy on 03/16/2003 12:00amOriginally posted by: zhanglixin
http://www.ietf.org/rfc/rfc2326.txt
here you can find theinformaiton of rtsp controls
let us share it..
ReplyHow can i download binary file like image?
Posted by Legacy on 03/13/2002 12:00amOriginally 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.
Reply-Monir
Monir@tigerit.com
How can I check first if the file exists
Posted by Legacy on 02/27/2002 12:00amOriginally 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
Replyfixed memory leak with GetFile() function
Posted by Legacy on 01/20/2002 12:00amOriginally 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)
Reply{
error->GetErrorMessage(szCause,254,NULL);
Cause.Format("%s",szCause);
}
END_CATCH_ALL;
return (Cause);
}
What about downloading from security servers?
Posted by Legacy on 10/17/2001 12:00amOriginally posted by: Anthony
ReplyThanks!!
Posted by Legacy on 08/14/2001 12:00amOriginally posted by: Jethro Kangleon
Thanks a lot for the code!
ReplyHow about under Windows 3.1.
Posted by Legacy on 07/30/2001 12:00amOriginally 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?
ReplyHow can i get the status of a file transfer?
Posted by Legacy on 05/09/2001 12:00amOriginally posted by: Sasikala.V.B.
How can i get the status of a file transfer
Reply(whether it is transfered or not)?
Loading, Please Wait ...