WinINet 将文件下载到客户端时遇到问题?

发布于 2024-09-15 19:10:54 字数 1569 浏览 12 评论 0原文

我很好奇为什么我在使用这个功能时遇到问题。我正在将网络上的 PNG 文件下载到目标路径。例如,将 Google 图片下载到 C: 盘:

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

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

这是代码:

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

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");

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!

Here's the code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁〃客ぐ 2024-09-22 19:10:54

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

// 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); 

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); 
饭团 2024-09-22 19:10:54

看起来 fwrite 的第二个和第三个参数被调换了。有关说明,请参阅 fwrite 文档

尝试:

fwrite(sTemp, 1, size, fp);

It looks like your second and third args to fwrite are transposed. See fwrite docs for explanation.

try:

fwrite(sTemp, 1, size, fp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文