Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.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.OpenRead分块下载数据_C#_Stream_Webclient_Chunks - Fatal编程技术网

C# WebClient.OpenRead分块下载数据

C# WebClient.OpenRead分块下载数据,c#,stream,webclient,chunks,C#,Stream,Webclient,Chunks,我正在尝试使用Webclient对象以每个5%的块下载数据。原因是我需要报告每个下载块的进度 以下是我为完成此任务而编写的代码: private void ManageDownloadingByExtractingContentDisposition(WebClient client, Uri uri) { //Initialize the downloading stream Stream str = client.OpenRead(uri.P

我正在尝试使用Webclient对象以每个5%的块下载数据。原因是我需要报告每个下载块的进度

以下是我为完成此任务而编写的代码:

    private void ManageDownloadingByExtractingContentDisposition(WebClient client, Uri uri)
    {
        //Initialize the downloading stream 
        Stream str = client.OpenRead(uri.PathAndQuery);

        WebHeaderCollection whc = client.ResponseHeaders;
        string contentDisposition = whc["Content-Disposition"];
        string contentLength = whc["Content-Length"];
        string fileName = contentDisposition.Substring(contentDisposition.IndexOf("=") +1);

        int totalLength = (Int32.Parse(contentLength));
        int fivePercent = ((totalLength)/10)/2;

        //buffer of 5% of stream
        byte[] fivePercentBuffer = new byte[fivePercent];

        using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
        {
            int count;
            //read chunks of 5% and write them to file
            while((count = str.Read(fivePercentBuffer, 0, fivePercent)) > 0);
            {
                fs.Write(fivePercentBuffer, 0, count);
            }
        }
        str.Close();
    }
问题是,当它到达str.Read()时,它暂停的次数与读取整个流的次数一样多,然后计数为0。因此,while()不起作用,即使我指定只读的值与fivePercent变量一样多。它看起来就像是在第一次尝试时读取了整个流

如何使其正确读取块

谢谢


Andrei

如果您不需要精确的5%块大小,您可能需要研究异步下载方法,例如或

每次下载新数据和进度更改时,它们都会触发该事件,该事件在事件参数中提供完成百分比

一些示例代码:

WebClient client = new WebClient();
Uri uri = new Uri(address);

// Specify a progress notification handler.
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);

client.DownloadDataAsync(uri);

static void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
    // Displays the operation identifier, and the transfer progress.
    Console.WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...", 
        (string)e.UserState, 
        e.BytesReceived, 
        e.TotalBytesToReceive,
        e.ProgressPercentage);
}

如果您不需要精确的5%块大小,您可能需要研究异步下载方法,例如或

每次下载新数据和进度更改时,它们都会触发该事件,该事件在事件参数中提供完成百分比

一些示例代码:

WebClient client = new WebClient();
Uri uri = new Uri(address);

// Specify a progress notification handler.
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);

client.DownloadDataAsync(uri);

static void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
    // Displays the operation identifier, and the transfer progress.
    Console.WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...", 
        (string)e.UserState, 
        e.BytesReceived, 
        e.TotalBytesToReceive,
        e.ProgressPercentage);
}

您的while循环在行的末尾有一个分号。我很困惑为什么被接受的答案是正确的,直到我注意到。

你的while循环在这行的末尾有一个分号。我很困惑为什么接受的答案是正确的,直到我注意到。

注册WebClient.DownloadProgressChanged事件,然后调用WebClient.DownloadDataAsync()。您可以在事件回调中更新进度。注册WebClient.DownloadProgressChanged事件,然后调用WebClient.DownloadDataAsync()。您可以在事件回调中更新进度。谢谢您的建议,但应用程序不允许异步下载。我必须找到一种处理同步下载进度报告的方法。你能澄清一下“应用程序不允许异步下载”是什么意思吗?您的应用程序是如何防止这种情况发生的?我的应用程序需要下载多个文件,并且需要同步下载,一次一个,这只是一个任务请求。最初,我使用webclient.DownloadFile()进行同步下载,但我还需要报告进度,因此我想到使用webclient.OpenRead()来报告区块。@Andrei您可以尝试链接下载请求。下载一个文件,然后在下载完成后,为事件处理程序中的下一个文件调用DownloadDataAsync。虽然我同意你的观点,也理解你的意思,而且我同意这是最明显和最简单的解决方案,但我使用的应用程序处于如此高级的级别,切换到异步下载将需要一段时间的开发和重新测试,这是客户完全无法接受的。这就是为什么我希望在我发布的代码中有一些错误可以发挥作用。谢谢你的建议,但是应用程序不允许异步下载。我必须找到一种处理同步下载进度报告的方法。你能澄清一下“应用程序不允许异步下载”是什么意思吗?您的应用程序是如何防止这种情况发生的?我的应用程序需要下载多个文件,并且需要同步下载,一次一个,这只是一个任务请求。最初,我使用webclient.DownloadFile()进行同步下载,但我还需要报告进度,因此我想到使用webclient.OpenRead()来报告区块。@Andrei您可以尝试链接下载请求。下载一个文件,然后在下载完成后,为事件处理程序中的下一个文件调用DownloadDataAsync。虽然我同意你的观点,也理解你的意思,而且我同意这是最明显和最简单的解决方案,但我使用的应用程序处于如此高级的级别,切换到异步下载将需要一段时间的开发和重新测试,这是客户完全无法接受的。所以这就是为什么我希望我发布的代码中有一些错误可以发挥作用。这很有效,谢谢。但我不明白为什么它会起作用。可能是因为计数后的时间被修改了。这很有效,谢谢。但我不明白为什么它会起作用。可能是因为修改了while is after count。