在标签中获取http文件大小,下载位置和URL [英] Get http file size, download location, and URL in a label

查看:158
本文介绍了在标签中获取http文件大小,下载位置和URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用它下载文件并获取%信息和完整信息。我想知道如何获取要下载的文件的大小以及URL远程地址和文件保存到的本地地址。

I am using this to download a file and get % info and completed info. I want to know how to get the size of the file being downloaded and the URL remote address and the local address where the file is being saved to.

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";
        }


推荐答案

我只是改写了Jay写的东西作为对自己问题的注释,以便于阅读:

I just rewrote what Jay wrote as comments to his own question, so that it'll be easier to read:

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;

只是意识到这正是如何从http标头中获取文件大小(也许这个问题应该标记为重复的?)。

Just realized it's exactly what's in How to get the file size from http headers (maybe this question should be marked as duplicate?).

这篇关于在标签中获取http文件大小,下载位置和URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆