简体   繁体   English

如何检查HttpWebRequest上传的进度?

[英]How can I check the progress of a HttpWebRequest upload?

I wonder how do I check how much of a file has been uploaded/downloaded? 我想知道如何检查已上传/下载了多少文件? I am using HttpWebRequest 我正在使用HttpWebRequest

You can do this is you use async mode on the HttpWebRequest - there is a working sample (based on the MSDN doc sample code) here . 你可以做到这一点,你使用异步模式在HttpWebRequest -有一个工作示例(根据MSDN文档示例代码) 在这里 Brief description: 简要描述;简介:

Here's a little Win Forms client that allows you to download a single file from a server, using either HTTP or FTP. 这是一个小的Win Forms客户端,它允许您使用HTTP或FTP从服务器下载单个文件。 It shows download progress and displays the average transfer rate, in kb/sec. 它显示下载进度并显示平均传输速率,以kb / sec为单位。 It also demonstrates how to use the HttpWebRequest and FtpWebRequest classes in System.Net to do file downloads. 它还演示了如何使用System.Net中的HttpWebRequest和FtpWebRequest类进行文件下载。

As long as you set either HttpWebRequest.ContentLength or HttpWebRequest.SendChunked before calling GetRequestStream, the data you send will be sent to the server with each call to Stream.[Begin]Write. 只要在调用GetRequestStream之前设置HttpWebRequest.ContentLength或HttpWebRequest.SendChunked,每次发送Stream时,您发送的数据就会发送到服务器。[Begin] Write。 If you write the file in small chunks suggests, you can get an idea of how far along you. 如果按照建议将文件分小块写入,则可以大致了解到文件的长度。

You have to call it asynchronously to update the progress of your upload/download. 您必须异步调用它以更新上载/下载进度。

HttpWebRequest have methods like HttpWebRequest具有类似的方法

public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state);
public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state);

accepting asynchronous callbacks. 接受异步回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM