在 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/