Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return xlsx data in Task<IHttpActionResult> with EPPlus

I have a basic web api that is trying to return an excel document triggered as a download action rather than a data stream. I have followed many examples online in regards to creating the stream and setting attachment properties but my Postman requests always show 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 code above if I use MediaTypeHeaderValue("application/pdf") then the Postman request shows the request to download but for the xlsx format or application/octet-stream it does not. Instead it shows a stream of data. The Content-Type is application/json because the final version of the api will be taking json data in the body.

Postman request

like image 241
Jeff Fol Avatar asked Oct 17 '22 08:10

Jeff Fol


1 Answers

I might be late, but... There is "Send and Download" in dropdown menu on Send button enter image description here

like image 58
Mike Dudnik Avatar answered Nov 15 '22 05:11

Mike Dudnik