C#WebClient下载空文件

本文关键字:文件 下载 C#WebClient | 更新日期: 2023-09-27 18:22:08

我正在开发一款使用必应API搜索和下载图像的应用程序。必应的API提供了一组图像链接,我对它们进行迭代并下载每一个。

我遇到的问题是,有时下载的文件大小是0Kb。我认为发生这种情况是因为WebClient首先创建文件名,然后尝试写入文件名。因此,当它由于某种原因无法写入文件名时,就会发生这种情况。问题是,它在不引发异常的情况下发生,所以我的"Catch"语句无法捕获并删除该文件。

public void imageFetcher(string performerName, int maxNumberOfImages, RichTextBox richTextBox)
    {
        string performersDirPath = Environment.CurrentDirectory + @"'Performers'";
        string performerPath = performersDirPath + performerName + @"'";
        if (!Directory.Exists(performersDirPath))
        {
            Directory.CreateDirectory(performersDirPath);
        }
        if (!Directory.Exists(performerPath))
        {
            Directory.CreateDirectory(performerPath);
        }
        // Searching for Images using bing api
        IEnumerable<Bing.ImageResult> bingSearch = bingImageSearch(performerName);
        int i = 0;
            foreach (var result in bingSearch)
            {
                downloadImage(result.MediaUrl, performerPath + performerName + i + ".jpg",richTextBox);
                i++;
                if (i == maxNumberOfImages)
                {
                    break;
                }
            }
    }

下载方式:

public void downloadImage(string imgUrl, string saveDestination, RichTextBox richTextBox)
    {
        if (File.Exists(saveDestination))
        {
            richTextBox.ForeColor = System.Drawing.Color.Red;
            richTextBox.AppendText("The File: " + saveDestination + "Already exists");
        }
        else
        {
            try
            {
                using (WebClient client = new WebClient())
                    {
                    client.DownloadFileCompleted += new AsyncCompletedEventHandler(((sender, e) => downloadFinished(sender, e, saveDestination , richTextBox)));
                    Uri imgURI = new Uri(imgUrl, UriKind.Absolute);
                    client.DownloadFileAsync(imgURI, saveDestination);
                    }
            }
            catch (Exception e)
            {
                richTextBox.AppendText("There was an exception downloading the file" + imgUrl);
                richTextBox.AppendText("Deleteing" + saveDestination);
                File.Delete(saveDestination);
                richTextBox.AppendText("File deleted!");
            }
        }
    }

当我尝试等待客户端完成使用时也会发生这种情况:

client.DownloadFileAsync(imgURI, saveDestination);
                        while (client.IsBusy)
                        {
                        }

有人能告诉我我做错了什么吗?

在另一个模拟问题中,解决方案是保持Web客户端实例打开,直到下载完成。。我用这个循环来做这个:

while (client.IsBusy){}

然而结果是一样的。

更新:我没有使用网络客户端,而是使用了以下代码:

try
                        {
                            byte[] lnBuffer;
                            byte[] lnFile;
                            using (BinaryReader lxBR = new BinaryReader(stream))
                            {
                                using (MemoryStream lxMS = new MemoryStream())
                                {
                                    lnBuffer = lxBR.ReadBytes(1024);
                                    while (lnBuffer.Length > 0)
                                    {
                                        lxMS.Write(lnBuffer, 0, lnBuffer.Length);
                                        lnBuffer = lxBR.ReadBytes(1024);
                                    }
                                    lnFile = new byte[(int)lxMS.Length];
                                    lxMS.Position = 0;
                                    lxMS.Read(lnFile, 0, lnFile.Length);
                                }
using (System.IO.FileStream lxFS = new FileStream(saveDestination, FileMode.Create))
                                    {
                                        lxFS.Write(lnFile, 0, lnFile.Length);
                                    }

这几乎完美地解决了问题,仍然有一两个0KB的文件,但我认为这是因为网络错误。

C#WebClient下载空文件

要查看可能的异常-请尝试将DownloadFileAsync更改为仅DownloadFile-我的问题是"无法创建SSL/TLS安全通道"。希望这能帮助到别人。