Downloading an image
Hey guys, I am writing a program to download images.
But the catch is that i need the file name to be read from a .mdb file.
There are multiple filenames that I would need to pull from the database so that would need to be a variable.
So basically I am asking how you can download an image from the web with the filename being a variable.
Thank you ahead of time.
It's the same as if it was hardcoded. As for downloading, I don't know, but I'm sure you'll find plenty of sample code at codeprojects.com.
I wrote some code to download images for a test application before, so I can post the main function here which does most of the work. Keep in mind this code is Windows specific, so it won't do you much good if you are using other platforms.
int Download(char* szFile,char* szLink)
unsigned long clSize = 1000;
int totalTransferred = 0;
unsigned char *buff;
const int readBuffSize = 5000;
hInternet = InternetOpen("app",INTERNET_OPEN_TYPE_DIRECT,0,0,0);
hURL = InternetOpenUrl(hInternet,szLink,0,0,INTERNET_FLAG_NO_CACHE_WRITE,0);
MessageBox(NULL,"Unable to get content length","Error",0);
fileSize = atoi(contentLength);
buff = malloc(readBuffSize);
/* Open the file to write the contents */
hFileWrite = CreateFile(szFile,GENERIC_WRITE,0,0,OPEN_ALWAYS,0,0);
DWORD dwBytesWritten = 0;
MessageBox(NULL,"There was an error attemtping to download file.","ERROR",0);
if (n == 0)
/* EOF */
totalTransferred += n;
Note that this sample uses malloc/free, which is not recommended for C++. Instead use new/delete.
It is fine to use malloc/free in C-code that is included inside C++ code - there's as far as I know, nothing to suggest that new/delete does anything significantly different from malloc/free, and malloc/free is certainly not "bad" in any way - they are of course not recommended for NEW C++ code. But if you are using existing C-code in a C++ project [like the above code], then that's a valid use.
Originally Posted by Elysia