Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#WebClient下载空文件_C#_Webclient_Downloadfileasync - Fatal编程技术网

C#WebClient下载空文件

C#WebClient下载空文件,c#,webclient,downloadfileasync,C#,Webclient,Downloadfileasync,我正在开发一个应用程序,它使用Bing的API来搜索和下载图像。 Bing的API提供了一组图像链接,我对它们进行迭代并下载每一个 我遇到的问题是,有时下载的文件大小是0Kb。 我假设发生这种情况是因为WebClient首先创建文件名,然后尝试写入文件名。所以当它因为某种原因无法写入时,就会发生这种情况。问题是,它发生时没有抛出异常,因此我的“Catch”语句无法捕获该异常并删除该文件 public void imageFetcher(string performerName, int maxN

我正在开发一个应用程序,它使用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;
                }
            }
    }
当我尝试等待客户端完成使用时,也会发生这种情况:

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的文件,但我认为这是因为网络错误。

要查看可能的异常,请尝试将DownloadFileAsync更改为仅下载文件-我的问题是“无法创建SSL/TLS安全通道”。希望这将对某人有所帮助。

中的副本可能存在,解决方案是等待(client.IsBusy为false)。我这样做了,结果还是一样的。你解决了这个问题吗?我也面临着同样的问题。@是的,见上面更新的帖子。完美,非常完美!
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);
                                    }