WinINet 无法将文件下载到客户端? [英] WinINet trouble downloading file to client?

查看:48
本文介绍了WinINet 无法将文件下载到客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇为什么我在使用此功能时遇到问题.我正在将网络上的 PNG 文件下载到目标路径.例如,将 Google 映像下载到 C: 驱动器:

I'm curious why I'm having trouble with this function. I'm downloading a PNG file on the web to a destination path. For example, downloading the Google image to the C: drive:

netDownloadData("http://www.google.com/intl/en_ALL/images/srpr/logo1w.png", "c:\file.png");

netDownloadData("http://www.google.com/intl/en_ALL/images/srpr/logo1w.png", "c:\file.png");

下载后文件大小正确.没有任何返回错误.当我尝试打开它时,它不会显示图像.任何想法都有帮助.谢谢!

The file size is correct after downloading. Nothing returning false. When I try opening it it won't show the image. Any ideas are helpful. Thanks!

代码如下:

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;
}

推荐答案

String 是什么数据类型?避免将二进制数据存储在字符串中,因为数据中的 NULL 可能会导致问题.只需在读取缓冲区时写入缓冲区:

What data type is String? Avoid storing binary data in strings because NULLs in the data can potentially cause problems. Just write the buffer as and when you read it:

// Load information to file. 
fp = fopen(strDestPath, "wb");
if (fp == NULL)
    return false;

// Read file.
while (InternetReadFile(hFile, buffer, 1024, &dwRead))
{
    if (dwRead == 0)
        break;

    fwrite(buffer, dwRead, 1, fp);
}

fclose(fp); 

这篇关于WinINet 无法将文件下载到客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆