在使用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
我唯一的线索是这个网站,它展示了
BeginCode
的实现。只有当count
小于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上的公共存储区,并通过常规的http请求获取。
source
是一个 Stream 对象。我会澄清的,谢谢! - Dorival_fileBytes
的类型是什么? - Cᴏʀʏ_fileBytes
是应该下载的文件的总大小。这部分代码仅用于显示下载进度。它是一个double
类型。 - Dorival