【发布时间】:2018-01-07 15:43:53
【问题描述】:
我有一个基本的 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("application/pdf") 那么 Postman 请求会显示下载请求,但对于 xlsx 格式或 application/octet-stream 则不会。相反,它显示了一个数据流。 Content-Type 是 application/json 因为最终版本的 api 将在 body 中获取 json 数据。
【问题讨论】:
标签: excel api postman xlsx epplus