Jay*_*Jay 1 c# webclient download winforms progress-bar
我正在使用它来下载文件并获取%信息和完成的信息.我想知道如何获取正在下载的文件的大小以及URL远程地址和保存文件的本地地址.
private void Form1_Load_1(object sender, EventArgs e)
{
label21.Text = "Download in progress...";
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri("http://www.somesite.com/Update/Updates.zip.010"), @"Updates.zip.010");
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage; //Progress Bar Handler
label1.Visible = true;
label1.Text = progressBar1.Value.ToString() + " %"; //Adds percent to a label
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
label11.Visible = true;
label11.Text = "Done";
}
Run Code Online (Sandbox Code Playgroud)
我只是重写了杰伊写给他自己的问题的评论,所以它会更容易阅读:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://cdn.sstatic.net/stackoverflow/img/favicon.ico");
req.Method = "HEAD";
// HttpWebRequest.GetResponse(): From MSDN: The actual instance returned
// is an HttpWebResponse, and can be typecast to that class to access
// HTTP-specific properties.
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
long len = resp.ContentLength;
Run Code Online (Sandbox Code Playgroud)
刚刚意识到它正是如何从http标题中获取文件大小(也许这个问题应该标记为重复?).
| 归档时间: |
|
| 查看次数: |
9462 次 |
| 最近记录: |