我有一个基本的web api,它试图返回一个作为下载动作而不是数据流触发的excel文档。我在网上关注创建流和设置附件属性的许多示例,但我的Postman请求始终显示为已编码。以下是API方法。
[Route("getexcel")]
[HttpPost]
public async Task<IHttpActionResult> GetExcel()
{
IHttpActionResult result = null;
FileInfo newFile = new FileInfo(@"C:\Test.xlsx");
ExcelPackage pck = new ExcelPackage(newFile);
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new ByteArrayContent(pck.GetAsByteArray());
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.Content.Headers.ContentDisposition.FileName = "Test.xlsx";
result = ResponseMessage(response);
return result;
}
如果我使用MediaTypeHeaderValue(&#34; application / pdf&#34;),则从上面的代码中,Postman请求显示下载请求,但对于xlsx格式或application / octet-stream,它不会。相反,它显示了一个数据流。 Content-Type是application / json,因为api的最终版本将在正文中获取json数据。