首页 > 解决方案 > aspnet core - 浏览器未在文件结果中显示下载进度

问题描述

我有一个简单的网络应用程序,它允许下载文件。
该文件被正确下载,但是浏览器从不指示接收到的数据量(即进度)。

它只是一直显示 0.1 MB,直到文件突然被下载。
在此处输入图像描述

下载动作的实现如下 - 现在,只需打开读取本地文件并将流作为文件结果返回。

public async Task<IActionResult> Download(string id)
{
    var project = await this.service.GetById(id).ConfigureAwait(false);
    if (project == null)
    {
        return this.NotFound($"Project [{id}] does not exist");
    }
    var file = new FileInfo(project.DownloadLocation);
    this.Response.ContentLength = file.Length;
    return this.File(file.OpenRead(), "application/octet-stream", file.Name);
}

下载由一个简单的操作链接触发:

 <dd class="project-path">@Html.ActionLink("Here",  "Download", "Download", new { id = Model.Id})</dd>

知道为什么 Chrome/Firefox 从不显示下载进度吗?

标签: c#asp.net-coremodel-view-controllerwebapi

解决方案


您的代码应该可以解决问题。您应该尝试使用更大的文件(例如:200mb)吗?这可能是计算 ETA 所花费的时间比在本地开发环境中下载更多的原因。


推荐阅读