Silverlight WebClient 渐进式下载

发布于 2024-07-25 04:08:35 字数 834 浏览 5 评论 0原文

我正在尝试逐步下载一系列序列化数据。 目标是从服务器发送单个大块,并在下载时在客户端上部分处理它。

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

貪欢 2024-08-01 04:08:35

不要使用 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

明月松间行 2024-08-01 04:08:35

您是否在 IE 上下载小于 4kb 的数据? 除非您的数据超过 4kb,否则 IE 不会为您提供数据。 4kb 之后,您就拥有了所需的所有粒度。 可能的解决方案:

  • 发送垃圾数​​据以达到 4kb
  • 如果您知道请求会很小,请将AllowReadStreamBuffering 设置为true。

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:

  • Send garbage data to get up to 4kb
  • If you know the request is going to be small, set AllowReadStreamBuffering to true.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文