MattSayar
MattSayar

Reputation: 2088

ASP.NET/C# - Detect File Size from Other Server?

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?

Upvotes: 4

Views: 2316

Answers (2)

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421998

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.

Upvotes: 5

Mitchel Sellers
Mitchel Sellers

Reputation: 63126

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

Upvotes: 0

Related Questions