我有一个向用户发送文件的 ASP.NET Core 1.2 应用程序。我FileResult
为此目的使用 a 。我的代码如下所示:
return PhysicalFile(Path.GetFullPath(filePath), QMimeTypeMap.GetMimeType(Path.GetExtension(file.Name)));
我曾尝试使用https://github.com/aspnet/BasicMiddleware/blob/dev/samples/ResponseBufferingSample/Startup.cs#L17中的响应缓冲,但它对我不起作用。
这将正确返回文件,但使用Transfer-Encoding: Chunked
,这会导致下载以“不确定”形式出现,即没有进度条。我试过Content-Length
自己设置标题,但它会自动删除。
编辑:请注意,如上所述,我已经尝试过响应缓冲。这并没有解决我的问题。
编辑 2:以下是我使用的代码的 SSCCE
FilesController.cs > 动作
[HttpGet("files")]
public async Task<IActionResult> Download(string driveName, [Bind(Prefix = "id")] uint? fileId = null, [Bind(Prefix = "download")] int forceDownload = 0)
{
// Send file info
string filePath = "...";
HttpContext.Response.Headers["Content-Length"] = new System.IO.FileInfo(filePath).Length.ToString();
return PhysicalFile(Path.GetFullPath(filePath), QMimeTypeMap.GetMimeType(Path.GetExtension(filePath)));
}
Startup.cs > 配置
app.UseStaticFiles();
app.UseResponseBuffering();
app.UseMvc();