简体   繁体   English

FileStreamResult上的ASP.NET Core响应标头

[英]ASP.NET Core Response headers on a FileStreamResult

I am doing a .mp4 file download from Azure Blob storage and pushing that to the UI. 我正在从Azure Blob存储下载.mp4文件,并将其推送到UI。 The download works fine, the issue is it doesn't look like the headers are set correctly for content-length. 下载工作正常,问题在于它似乎没有为内容长度正确设置标题。 Thus you cannot track the download time because the browser only says what has been downloaded and not how much is left and the estimated time. 因此,您无法跟踪下载时间,因为浏览器仅显示已下载的内容,而不显示剩余的时间和估计的时间。 Is my code for the response wrong or should I change my request? 我的响应代码有误还是应该更改请求? My code as follows: 我的代码如下:

[HttpGet("VideoFileDownload")]
public IActionResult VideoFileDownloadAsync([FromQuery]int VideoId)
{
     ...code to get blob file
     return new FileStreamResult(blob.OpenRead(), new MediaTypeHeaderValue("application/octet-stream")
}

I have played around with various request and response headers but it makes no difference. 我玩过各种各样的请求和响应头,但这没什么区别。

The files are big and I know the old asp.net way of checking for range headers and then do a chunked stream but I want to use the new features in .net core which doesn't work as expected or maybe I just don't understand it thoroughly. 文件很大,我知道检查范围标头的旧asp.net方法,然后执行分块流,但是我想使用.net core中的新功能,该功能无法按预期工作,或者也许我只是不彻底了解它。 Can somebody give me a working sample of a file download with asp.net core code. 有人可以给我一个带有asp.net核心代码的文件下载示例。

If you have the file size, you can set the response's content length in the Response object just before returning the FileStreamResult, like this: 如果具有文件大小,则可以在返回FileStreamResult之前在Response对象中设置响应的内容长度,如下所示:

[HttpGet("VideoFileDownload")]
public IActionResult VideoFileDownloadAsync([FromQuery]int VideoId)
{
     ...code to get blob file
     long myFileSize = blob.Length; // Or wherever it is you can get your file size from.
     this.Response.ContentLength = myFileSize;
     return new FileStreamResult(blob.OpenRead(), new MediaTypeHeaderValue("application/octet-stream")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM