Returns xlsx data in Task <IHttpActionResult> using EPPlus
I have a basic web api that is trying to return an excel document triggered as a load action, not a data stream. I have followed many examples online regarding stream creation and setting binding properties, but my Postman requests always show up as encoded. Below is the API method.
[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;
}
From the above code, if I use MediaTypeHeaderValue ("application / pdf") Postman request shows download request, but for xlsx format or application / octet stream this is not the case. Instead, it shows the data flow. Content-Type is application / json because the final version of the api will accept json data in the body.
+3
source to share