[ACCEPTED]-Get Size of Image File before downloading from web-.net
Here is a simple example you can try if 3 you have files of different extensions like 2 .GIF, .JPG, etc you can create a variable 1 or wrap the code within a Switch Case Statement
System.Net.WebClient client = new System.Net.WebClient();
client.OpenRead("http://someURL.com/Images/MyImage.jpg");
Int64 bytes_total= Convert.ToInt64(client.ResponseHeaders["Content-Length"])
MessageBox.Show(bytes_total.ToString() + " Bytes");
If the web-service gives you a Content-Length HTTP header 4 then it will be the image file size. However, if 3 the web-service wants to "stream" data 2 to you (using Chunk encoding), then you 1 won't know until the whole file is downloaded.
You can use this code:
using System.Net;
public long GetFileSize(string url)
{
long result = 0;
WebRequest req = WebRequest.Create(url);
req.Method = "HEAD";
using (WebResponse resp = req.GetResponse())
{
if (long.TryParse(resp.Headers.Get("Content-Length"), out long contentLength))
{
result = contentLength;
}
}
return result;
}
0
You can use an HttpWebRequest to query the 2 HEAD Method of the file and check the Content-Length 1 in the response
You should look at this answer: C# Get http:/…/File Size where your 5 question is fully explained. It's using 4 HEAD HTTP request to retrieve the file size, but 3 you can also read "Content-Length" header 2 during GET request before reading response 1 stream.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.