如果下载成功,为什么 WebClient.OpenReadAsync 会返回长度为零的 e.Result?
使用以下代码(来自 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 />
}
有什么想法吗?
Using the following code (from a Silverlight 4 OOB app) I'm getting a result stream with a size of zero even though it takes the time to download the whole file (900+MB) and no error is reported. Fiddler also says the whole file was downloaded.
The handler on progress changed (although not shown below) is hit and reports an increase in download percentage.
This works with smaller files (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 />
}
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(3)
情绪2024-10-03 07:32:43
数据下载的默认限制为 4Mb。你增加了吗?
试试这个链接:http://forums.silverlight.net/forums/p/21513 /75649.aspx
<system.web>
...
<httpRuntime maxRequestLength="xxx" /> // Size in Kb
</system.web>
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我的猜测是因为底层流是分块响应,并且 HTTP 响应中没有给出 Content Length 标头。因此,长度返回 0。在 中使用长度没有任何意义。类规范。根据规范:
所以我会忽略长度,检查这些字段,然后随心所欲地阅读。
My guess is because the underlying stream is a chunked response and no Content Length header was given in the HTTP response. So Length returns 0. There's nothing about using the Length in the class spec. According to the spec:
So I would ignore Length, check those fields, and then read to your heart's content.