Return Task<IHttpActionResult> 中的 xlsx 数据与 EPPlus
Return xlsx data in Task<IHttpActionResult> with EPPlus
我有一个基本网络 api,它正在尝试 return 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 的最终版本将在正文中获取 json 数据。
我可能会迟到,但是...
发送按钮的下拉菜单中有 "Send and Download"
我有一个基本网络 api,它正在尝试 return 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 的最终版本将在正文中获取 json 数据。
我可能会迟到,但是...
发送按钮的下拉菜单中有 "Send and Download"