使用EPPlus在任务<IHttpActionResult>中返回xlsx数据

我有一个基本的Web API,试图返回一个Excel文档,而不是一个数据stream作为下载操作触发。 关于创buildstream和设置附件属性,我已经在线了很多例子,但是我的邮差请求总是显示为编码。 以下是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(“应用程序/ pdf”),那么邮递员请求显示请求下载,但对于xlsx格式或应用程序/八位字节stream它不。 而是显示一个数据stream。 Content-Type是application / json,因为api的最终版本将会把json数据放在主体中。

邮递员请求

我可能会迟到,但是…发送button的下拉菜单中有“发送和下载” 在这里输入图像描述

Interesting Posts