tags:

views:

817

answers:

2

I'm trying to find the file size of a file on a server. The following code I got from this guy accomplishes that for your own server:

string MyFile = "~/photos/mymug.gif";

FileInfo finfo = new FileInfo(Server.MapPath(MyFile));
long FileInBytes = finfo.Length;
long FileInKB = finfo.Length / 1024;

Response.Write("File Size: " + FileInBytes.ToString() + 
  " bytes (" + FileInKB.ToString() + " KB)");

It works. However, I want to find the filesize of, for example:

string MyFile = "http://www.google.com/intl/en_ALL/images/logo.gif";

FileInfo finfo = new FileInfo(MyFile);

Then I get a pesky error saying URI formats are not supported.

How can I find the file size of Google's logo with ASP.NET?

+1  A: 

To get this value you would have to first download the file locally, then you can use the standard methods to get its size.

Mitchel Sellers
Dang. That's the ONLY way to do it?
Matt S
Mehrdad offered the only other alternative that I know of, but as he mentioned it is a bit sketchy in support
Mitchel Sellers
+3  A: 

You can use the WebRequest class to issue an HTTP request to the server and read the Content-Length header (you could probably use HTTP HEAD method to accomplish it). However, not all Web servers respond with a Content-Length header. In those cases, you have to receive all data to get the size.

Mehrdad Afshari