【发布时间】:2014-07-16 01:26:22
【问题描述】:
从 ASP.NET Web Api 2.x 控制器,我正在使用 StreamContent 类型的实例提供文件。当请求一个文件时,它的 blob 位于数据库中,并打开一个 blob 流。然后将 blob 流用作StreamContent 实例的输入。
归根结底,我的控制器动作类似于:
[HttpGet]
[Route("{blobId}")]
public HttpResponseMessage DownloadBlob(int blobId)
{
// ... find the blob in DB and open the 'myBlobStream' based on the given id
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StreamContent(myBlobStream)
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentLength = myBlobStream.Length;
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "foo.txt",
Size = myBlobStream.Length
};
return result;
}
当我在 Chrome (v. 35) 中点击端点时,它说它正在解析主机 (localhost),当文件下载后,它会出现在下载栏中。但是,我想知道需要什么才能启用 Chrome(或任何其他浏览器)来显示下载进度?
我认为这可以通过包含诸如内容类型、内容长度和内容处置等标头信息来解决,但从我的尝试来看,这没有任何区别。
【问题讨论】:
标签: c# asp.net browser stream asp.net-web-api2