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(uri开发者_开发问答Address, 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.
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.
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.
精彩评论