使用以下代码(来自 Silverlight 4 OOB 应用程序)我得到一个大小为零的结果流,即使下载整个文件 (900+MB) 需要时间并且没有报告错误。Fiddler 还说整个文件都已下载。
进度更改的处理程序(尽管下面未显示)被命中并报告下载百分比增加。
这适用于较小的文件 (10MB)。
var wc = new WebClient();
wc.OpenReadCompleted += DownloadWholeFileOpenReadCompleted;
wc.DownloadProgressChanged += DownloadWholeFileDownloadProgressChanged;
wc.OpenReadAsync(new Uri(movie.DownloadUrl, UriKind.Absolute));
private static void DownloadWholeFileOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Cancelled)
{
return; // this is not hit
}
if (e.Error != null)
{
return; // this is not hit
}
using (var fs = new FileStream(tempFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
{
var buffer = new byte[4096];
int bytesRead;
// <snip />
// e.Result.Length this equals 0
while ((bytesRead = e.Result.Read(buffer, 0, buffer.Length)) != 0)
{
fs.Write(buffer, 0, bytesRead);
}
fs.Close();
}
// <snip />
}
有任何想法吗?