tags:

views:

152

answers:

3

Hi SO,

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

Do web servers have accessible properties (fields) with regard to file sizes? This would allow for checking the image size without loading.

Often when a web server's directory is loaded into a browser, it tells you each file's size, from server-side. Can I, as an ASP developer, access that data at all?

I'm using C# .Net 4

Edit: I am acting as a client and requesting this info from other web servers.

Appreciate any help, as always. Alex

+2  A: 
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/want to do this strictly through HTTP, you should try to catch the Content-Length header that comes with the request. It may not always be there though, so you'll have to experiment with that and make sure that the web-server accurately states the file length. If it's an image from a script for example, the Content-Length header may be missing (in which case, shame on someone).

If the Content-Length is missing, then I'm pretty sure there's no way to determine how large the file is through HTTP without downloading it. Content-Length headers are used for exactly this sort of thing.

Helgi Hrafn Gunnarsson
@Helgi Hrafn Gunnarsson ... Fantastic news. I had the thought of HTTP header checking, and you've defined this perfectly through Content-Length. We can always skip any dodgy header data, but hopefully most of the content will be available. Thanks... will let you know how it goes when I implement tomorrow. Its 2.16am here!!! :)
AlexW
Shame on no-one if it's sent with `Transfer-Encoding: Chunked`, as then it MUST NOT (in the RFC 2119 sense of a capitalised "MUST NOT") be sent, and if it was sent it MUST be ignored.
Jon Hanna
Does System.IO.FileInfo work with web data or only local data? Can the filepath be a URI?
AlexW
local, or using UNC for a machine you have file-access too, not web.
Jon Hanna
+8  A: 

Do you mean when you are the server, or the client?

If you're the server, you can find it out whatever whay you could find it out about any other file (assuming the image stream is coming from a file).

new FileInfo(path).Length;

If you mean you're doing the client-code (you're accessing another webserver)

Do a HEAD request. While some servers don't behave correctly, the correct response to a HEAD is pretty much identical to that for a GET except that the entity isn't sent.

For example, to get the sprite PNG that is used on this page, the browser does a GET to http://sstatic.net/stackoverflow/img/sprites.png which results in the response:

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 the octets of the actual image file.

Doing a HEAD instead of a get results in:

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 entity body. At this point we can see that the image is 18607bytes in size, without actually downloading it. This method will not work though if the image would be sent chunked, as then the content-length would not be sent in a header.

Jon Hanna
I am acting as client yes. So the FileInfo will not work then I see. HEAD info is the way to go, despite the pitfalls mentioned here.
AlexW
Don't you mean 18607 bytes in size?
jeffora
LOL. I copy-pasted so I couldn't possibly mis-type it, and then I copy-pasted the max-age by mistake. Fixed, thanks.
Jon Hanna
Would this information be found in the `HttpWebResponse` object as `response.Headers["Content-Length"];` ? Will find out tomorrow I guess - Many thanks!
AlexW
It will indeed (I'm not sure about the ContentLength property, I vaguely remember that property behaving a bit strangely in cases like this).
Jon Hanna
A: 

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

koan