Get the image size without downloading it from the web server

Is it possible to get the size of an image (perhaps its size in bytes) from a web server without downloading it?

Do web servers have available properties (fields) regarding file sizes? This will allow you to check the image size without downloading.

Often, when the web server directory is loaded into the browser, it tells you each file size on the server side. Can I, as an ASP developer, access this data at all?

I am using C # .Net 4

Edit: I act as a client and request this information from other web servers.

Appreciate any help, as always. Alex

+7
c # image
source share
3 answers

Do you mean when you are a server or client?

If you are a server, you can find out everything that you could find in any other file (if the image stream comes from the file).

new FileInfo(path).Length; 

If you mean that you are doing client code (you are gaining access to another web server)

Make a HEAD request. Although some servers behave incorrectly, the correct response to HEAD is pretty much identical to the GET rule, except that the object has not been sent.

For example, to get the PNG sprite that is used on this page, the browser performs a GET at http://sstatic.net/stackoverflow/img/sprites.png , which leads to the answer:

 HTTP/1.1 200 OK Server: nginx Date: Tue, 17 Aug 2010 01:06:21 GMT Content-Type: image/png Connection: keep-alive Cache-Control: max-age=604800 Last-Modified: Tue, 13 Jul 2010 06:28:14 GMT Accept-Ranges: bytes X-Powered-By: ASP.NET Content-Length: 18607 

followed by octets of the real image file.

Doing HEAD instead of getting results:

 HTTP/1.1 200 OK Server: nginx Date: Tue, 17 Aug 2010 01:07:20 GMT Content-Type: image/png Connection: keep-alive Cache-Control: max-age=604800 Content-Length: 18607 Last-Modified: Tue, 13 Jul 2010 06:28:14 GMT Accept-Ranges: bytes X-Powered-By: ASP.NET 

pretty much the same , but without the body of the object . At this stage, we see that the image is 18607 bytes in size, without actual loading. This method will not work, although if the image is sent and then the length of the content will not be sent in the header.

Edit:

It is worth noting that sometimes with fragmented content you will have no choice but to download all this, because the server will not say (and do not even know) the size when sending it. Unfortunately, this is likely to be used with especially large threads. Fortunately, this is unlikely to be used with images.

+11
source share
 System.IO.FileInfo fi = new System.IO.FileInfo(filepath); fi.Length(); 

Psst, just a hint: http://www.google.ca/#hl=en&source=hp&q=c%23+get+file+size&aq=f&aqi=g4g-m2&aql=&oq=&gs_rfai=&fp=e6a957a020d2d8f6

If you need / need to do this strictly through HTTP, you should try to catch the Content-Length header that comes with the request. However, this may not always be the case, so you will have to experiment with this and make sure that the web server accurately determines the file length. If this is an image from a script, for example, the Content-Length header may be missing (in this case, disgrace someone).

If Content-Length is missing, then I am sure that there is no way to determine how large the file is through HTTP without downloading it. Content-Length headers are used for this kind of thing.

+2
source share

There are many web servers that do not serve the Content-Length field. You will need to make sure your code is tolerant of this.

+1
source share

All Articles