By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,896 Members | 2,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,896 IT Pros & Developers. It's quick & easy.

size of file located at url

P: n/a
My web app needs to determine the file size in bytes for image file urls
stored in db. Does .NET provide a method that can determine the file size in
bytes ?
Sep 21 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
John,

Not really. It completely depends on the protocol that the URL
represents. If the protocol is http, or https, then you can do this easily.

You can issue a HEAD request to an HTTP server resource and (assuming it
implements the HTTP 1.1 protocol) it should return a Content-Length header
which you can then use as the size of the file. However, this will only
work for static resources, as dynamically generated pages don't typically
populate this header.

Now, to do this in .NET, you can create a WebRequest/WebResponse for the
particular protocol using the static Create method on the WebRequest class.
Then, you can issue a call to GetResponse, and then access the ContentLength
property on the response that is returned to you.

If you are working with HTTP resources only, then you can do this:

// Create the request.
WebRequest request = WebRequest.Create(url);

// Set the method to HEAD.
request.Method = "HEAD";

// Get the response.
using (WebResponse response = request.GetResponse())
{
// Get the length.
long length = response.ContentLength;
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"John A Grandy" <johnagrandy-at-yahoo-dot-comwrote in message
news:uw**************@TK2MSFTNGP02.phx.gbl...
My web app needs to determine the file size in bytes for image file urls
stored in db. Does .NET provide a method that can determine the file size
in bytes ?

Sep 21 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.