我很好奇为什么我在使用此功能时遇到问题。我正在将网络上的 PNG 文件下载到目标路径。例如,将 Google 图像下载到 C: 盘:
netDownloadData(" http://www.google.com/intl/en_ALL/images/srpr/logo1w.png ", "c:\file.png");
下载后文件大小正确。没有返回错误。当我尝试打开它时,它不会显示图像。任何想法都是有帮助的。谢谢!
这是代码:
bool netDownloadData(const char *strSourceUrl, const char *strDestPath)
{
HINTERNET hINet = NULL;
HINTERNET hFile = NULL;
char buffer[1024];
DWORD dwRead;
String sTemp;
FILE *fp = NULL;
DWORD size = 0;
// Open a new internet session
hINet = netInit();
if (hINet == NULL) {
sprintf(buffer, "Initializing WinINet failed.", strSourceUrl);
utilLog(buffer);
netCloseHandle(hINet);
return false;
}
// Open the requested url.
hFile = netOpenUrl(hINet, strSourceUrl);
if (hFile == NULL) {
sprintf(buffer, "URL failed upon loading: %s\n", strSourceUrl);
utilLog(buffer);
netCloseHandle(hINet);
return false;
}
// Read file.
while (InternetReadFile(hFile, buffer, 1023, &dwRead))
{
if (dwRead == 0)
break;
buffer[dwRead] = 0;
sTemp += buffer;
size += dwRead;
}
// Load information to file.
fp = fopen(strDestPath, "wb");
if (fp == NULL)
return false;
fwrite(sTemp, size, 1, fp);
fclose(fp);
InternetCloseHandle(hFile);
InternetCloseHandle(hINet);
return true;
}