首页 文章

C#WebClient下载空文件

提问于
浏览
0

我正在开发一个使用Bing的API来搜索和下载图像的应用程序 . Bing的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)
                        {

                        }

谁能告诉我我做错了什么?

在其他类似的问题中,解决方案是保持Webclient实例打开,直到下载完成 . 我正在使用此循环执行此操作:

while (client.IsBusy){}

但结果是一样的 .

更新:我使用不使用webclient,而是使用此代码:

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文件,但我认为这是由于网络错误 .

1 回答

  • 1

    要查看可能的异常 - 尝试将DownloadFileAsync更改为DownloadFile - 我的问题是“无法创建SSL / TLS安全通道” . 希望这会对某人有所帮助 .

相关问题