c# - 通过 Stream.Read 下载文件时出现 ArgumentOutOfRangeException

标签 c# silverlight webclient

在 Silverlight 上下载非常大的文件 (>2GB) 时,我遇到了一个问题。我的应用程序是一个以提升的权限运行的浏览器外下载管理器。

当文件达到一定的数据量(2GB)时,抛出如下异常:

System.ArgumentOutOfRangeException was caught
  Message=Specified argument was out of the range of valid values.
Parameter name: count
  StackTrace:
   in MS.Internal.InternalNetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
   in MS.Internal.InternalNetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   in MySolution.DM.Download.BeginResponseCallback(IAsyncResult ar)
  InnerException: 
Null

我唯一的线索是 this site , 谁展示了 BeginCode 的实现。此异常仅在 count 为 < then 0 时发生。

我的代码

/* "Target" is a File object. "source" is a Stream object */

var buffer = new byte[64 * 1024];
int bytesRead;
Target.Seek(0, SeekOrigin.End); // The file might exists when resuming a download

/* The exception throws from inside "source.Read" */
while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
{
    Target.Write(buffer, 0, bytesRead);
    _fileBytes = Target.Length;
    Deployment.Current.Dispatcher.BeginInvoke(() => { DownloadPercentual = Double.Parse(Math.Round((decimal)(_fileBytes / (_totalSize / 100)), 5).ToString()); });
}

Target.Close();
logFile.Close();

错误发生在不同类型的文件上,它们来自 Amazon S3 上的公共(public)存储桶。 (使用常规的 http 请求)。

最佳答案

我搜索了一下,看起来这是 Silverlight 中的一个已知限制。一种可能的解决方法是使用 Range header 分多个部分执行下载,每个部分小于 2GB。

关于c# - 通过 Stream.Read 下载文件时出现 ArgumentOutOfRangeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12063142/

相关文章:

c# - 如何从非控制台应用程序项目写入控制台

Silverlight 可写位图

c# - Silverlight 中的简单 2D 'Space Invaders' 克隆

IIS 5 上的 Silverlight 5 WCF Ria 服务 404 错误

c# - 从 Webclient 调用标签 javascript 点击函数

c# - 为 webClient.DownloadFile() 设置超时

c# - 在 C# 中运行/生成命令与文件

c# - 如何在 ASP.NET 中获取提示值?

c# - 颜色 TextBlock 取决于枚举值

C# - 如何动态创建进度条