Gilad
Gilad

Reputation: 2886

Getting file size in Silverlight 4

I'm downloading (from server to client) a file using a WebClient object:

WebClient wc = new WebClient();
wc.OpenReadCompleted += Load_TransferCompleted;
wc.OpenReadAsync(uriAddress, Filename);

I would like to know the file size before starting the download operation. Is there a way to do this in SL4?

thanks for your help.

Gilad.

Upvotes: 2

Views: 725

Answers (2)

AnthonyWJones
AnthonyWJones

Reputation: 189505

Here is some air code for you to play with (I haven't tested it myself)

 WebRequest req = WebRequestCreator.ClientHttp.Create(yourUri);
 req.Method = "HEAD";
 req.BeginGetResponse(ar =>
 {
     WebResponse resp = req.EndGetResponse(ar);
     int length = resp.ContentLength;

     // Do stuff with length
 }, null);

By using the ClientHttp stack you can use "HEAD" request which will return the same set of headers as a "GET" but not the actual entity body.

There is at least one thing to look out for though, none of the existing cookies for the uri will be sent in the request. If the response is sensitive to cookies (for example because it needs a session id) then things will get a whole lot more complicated.

Upvotes: 1

Emond
Emond

Reputation: 50682

The only way I see to make this possible is by publishing the size. It could be coded but also obtainable through a web service.

Upvotes: 0

Related Questions