15

I am downloading image files from web using the following code in my Console Application.

WebClient client = new WebClient();
client.DownloadFile(string address_of_image_file,string filename);

The code is running absolutely fine.

I want to know if there is a way i can get the size of this image file before I download it.

PS- Actually I have written code to make a crawler which moves around the site downloading image files. So I doesn't know its size beforehand. All I have is the complete path of file which has been extracted from the source of webpage.

user1589754
  • 647
  • 2
  • 8
  • 20
  • Possible duplicate of [Get http:/.../File Size](http://stackoverflow.com/questions/122853/get-http-file-size) – Francis Nov 09 '16 at 16:21

5 Answers5

24

Here is a simple example you can try if you have files of different extensions like .GIF, .JPG, etc you can create a variable 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");
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • This works like a charm for me, but after the third use it stops everything and crashes. I even tried calling client.Dispose() but it's not helping... Do you have any idea why that might be happening? Thanks! – Thpramos May 19 '15 at 20:14
  • 1
    client.OpenRead() opens a readable stream for the data downloaded. So it have to be closed after use. Something like this prevents your app from crashing: `using (var sr = client.OpenRead(uri)) { ... }` – jurajvt Apr 04 '18 at 06:22
  • When I want to download using the same webclient, should I close the Opened connection (to read the size), or what should I exactly do? – Mohammed Noureldin Apr 22 '18 at 20:28
6

If the web-service gives you a Content-Length HTTP header then it will be the image file size. However, if the web-service wants to "stream" data to you (using Chunk encoding), then you won't know until the whole file is downloaded.

EthanB
  • 4,239
  • 1
  • 28
  • 46
5

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;
}
Ghost4Man
  • 1,040
  • 1
  • 12
  • 19
seyed
  • 71
  • 1
  • 7
2

You can use an HttpWebRequest to query the HEAD Method of the file and check the Content-Length in the response

Francis
  • 3,335
  • 20
  • 46
0

You should look at this answer: C# Get http:/…/File Size where your question is fully explained. It's using HEAD HTTP request to retrieve the file size, but you can also read "Content-Length" header during GET request before reading response stream.

Community
  • 1
  • 1
Perevalov
  • 338
  • 1
  • 6
  • -1. Either make it comment or add summary of the link to the answer. – Alexei Levenkov Aug 22 '12 at 19:37
  • Question and answer in specified link need no explanation and it's difficult to add something. I don't want to copy-paste answer from original post. Anyway, thanks for the comment, I'll take it into account in the future. – Perevalov Aug 22 '12 at 19:47
  • Welcome to SO. SO answers are expected to contain summary of a link - check [FAQ](http://stackoverflow.com/questions/how-to-answer) - "Provide context for links". – Alexei Levenkov Aug 22 '12 at 20:21