我需要创建一个下载文件的 WebAPI 方法。文件正在正确下载,但下载似乎在客户端浏览器中几乎保持在 100% 的状态(即,尽管文件已经下载并且可以打开,但它不会终止)。在终止之前保持这种状态几分钟。问题可能是什么?以下是一些复制我的问题的测试代码
[HttpGet]
public HttpResponseMessage GetFile()
{
string fileName = @"c:\temp\test.zip";
MemoryStream responseStream = new MemoryStream();
using (FileStream source = File.Open(fileName, FileMode.Open))
source.CopyTo(responseStream);
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StreamContent(responseStream);
response.StatusCode = HttpStatusCode.OK;
response.Content.Headers.ContentType = new MediaTypeHeaderValue(ContentTypeHelper.Instance.GetFileContentType(fileName));
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = fileName;
response.Content.Headers.ContentLength = responseStream.Length;
return response;
}