Silverlight WebClient 渐进式下载
我正在尝试逐步下载一系列序列化数据。 目标是从服务器发送单个大块,并在下载时在客户端上部分处理它。
我正在使用 System.Net.WebClient 类并将其AllowReadStreamBuffering 属性设置为 false。 根据 MSDN 文档,这应该允许我访问来自 OpenReadCompleted 事件的传入流。
然而,当我尝试访问该流时,它会抛出 NotSupportedException。 这不是跨域策略问题,如果我将AllowReadStreamBuffering 属性设置为true,它将完美加载和读取内容。 我错过了什么吗? 我应该如何从 Silverlight 执行渐进式下载?
复制此问题的最少代码如下:
private void BeginProgressiveDownload()
{
WebClient client = new WebClient();
client.AllowReadStreamBuffering = false;
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri("http://STREAMABLE RESOURCE HERE"));
}
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
e.Result.ReadByte();
}
I'm trying to progressively download an array of serialised data. The goal is to send a single large block from the server, and partially process it on the client whilst it downloads.
I'm using the System.Net.WebClient class and setting it's AllowReadStreamBuffering property to false. According to the MSDN documentation, this should allow me to access the incoming stream from the OpenReadCompleted event.
When I attempt to access the stream, however, it throws a NotSupportedException. This is not a cross-domain policy issue, and if I set the AllowReadStreamBuffering property to true it loads and reads the content perfectly. Am I missing something? How should I perform progressive downloads from Silverlight?
The minimal code to replicate this problem is this:
private void BeginProgressiveDownload()
{
WebClient client = new WebClient();
client.AllowReadStreamBuffering = false;
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri("http://STREAMABLE RESOURCE HERE"));
}
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
e.Result.ReadByte();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用 WebClient,而是使用套接字(如果可能的话)。Mike
Schwarz 有一个出色的套接字客户端,您可以使用
http://weblogs.asp.net/mschwarz/archive/2008/03/07/silverlight-2-and-sockets.aspx
Don't use WebClient for this, but rather sockets(if possible.)
Mike Schwarz has an excellent socket client you can use
http://weblogs.asp.net/mschwarz/archive/2008/03/07/silverlight-2-and-sockets.aspx
您是否在 IE 上下载小于 4kb 的数据? 除非您的数据超过 4kb,否则 IE 不会为您提供数据。 4kb 之后,您就拥有了所需的所有粒度。 可能的解决方案:
Are you on IE and downloading less than 4kb of data? IE won't give you the data until you have more than 4kb of it. After 4kb, you have all the granularity you need. Possible solutions: