public HttpResponseMessage Execute(string ZZtablename, string filterField, DateTime?beginDate, DateTime?endDate) { var includeOptions = this.Request.GetQueryNameValuePairs() .ToDictionary(kv => kv.Key, kv => kv.Value, StringComparer.OrdinalIgnoreCase); includeOptions.Remove("filterField"); includeOptions.Remove("beginDate"); includeOptions.Remove("endDate"); var o = new SearchOptions { name = ZZtablename, exportFilterField = filterField == "undefined" ? null : filterField, beginDate = beginDate, endDate = endDate, exportIncludeOptions = includeOptions }; // http://epplus.codeplex.com/wikipage?title=WebapplicationExample // https://stackoverflow.com/questions/30570336/export-to-excel-as-a-response-in-web-api byte[] bytes = null; serv.getXlsxFile(o, ref bytes); HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new ByteArrayContent(bytes); result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment"); result.Content.Headers.ContentDisposition.FileName = ZZtablename + ".xlsx"; result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/ms-excel"); result.Content.Headers.ContentLength = bytes.Count(); result.StatusCode = System.Net.HttpStatusCode.OK; return(result); }
public IActionResult Execute( [FromRoute] string tablename, [FromQuery] string filterField, [FromQuery] DateTime?beginDate, [FromQuery] DateTime?endDate) { Enum.TryParse <ValidTableNames>(tablename, out var unused); // validate that we've only received a table name var includeOptions = Request.Query.ToDictionary(kv => kv.Key, kv => kv.Value.ToString()); // TODO so sketch includeOptions.Remove("filterField"); includeOptions.Remove("beginDate"); includeOptions.Remove("endDate"); var o = new SearchOptions { name = tablename, exportFilterField = filterField == "undefined" ? null : filterField, // TODO Enum beginDate = beginDate, endDate = endDate, exportIncludeOptions = includeOptions }; // http://epplus.codeplex.com/wikipage?title=WebapplicationExample // https://stackoverflow.com/questions/30570336/export-to-excel-as-a-response-in-web-api byte[] bytes = null; serv.getXlsxFile(o, ref bytes); // HttpResponseMessage response = new HttpResponseMessage(); // response.Content = new ByteArrayContent(bytes); // response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); // response.Content.Headers.ContentDisposition.FileName = tablename + ".xlsx"; // response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/ms-excel"); // response.Content.Headers.ContentLength = bytes.Length; // response.StatusCode = HttpStatusCode.OK; // return new ResponseMessageResult(response); //<~ loses the content //return File(bytes, "application/ms-excel"); //<~ WORKS but JavaScript complains it is not Json return(new FileContentResult(bytes, new MediaTypeHeaderValue("application/ms-excel"))); // File() calls it }