我在控制器中有以下代码来返回一个流,该流是要在客户端下载的 Excel 文件:
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
MemoryStream memStream = ReportExporter.ExportReport(analystReport);
response.Content = new StreamContent(memStream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = saveAsFileName;
return response;
当我在客户端发出请求时,它只是将响应消息的包装返回给我:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Content-Type: application/octet-stream
Content-Disposition: attachment; filename=.xlsx
}
我相信这可能是由于标题以文本形式返回,因为这是我在 Chrome 中看到的:
Content-Type:text/html; charset=utf-8
尝试了各种方法后,我无法弄清楚这里发生了什么。