问题
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";
}
回答1:
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;
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?).
来源:https://stackoverflow.com/questions/3741814/get-http-file-size-download-location-and-url-in-a-label