I am building a FileResult method which is supposed to return an excel file as a result. Currently the method looks like this:
public ActionResult GetExcelFile(int id)
{
var entityPermission = AuthenticationManager.GetEntityPermission(PermissionEntity.Events, PermissionLevel.AllRecords);
if (entityPermission == null || !entityPermission.AllowRead)
return Json(new RequestResult(RequestCode.Unauthorized), JsonRequestBehavior.AllowGet);
try
{
using (var serviceManager = new ServiceManager())
{
// Get object model
var firstTableObj = serviceManager.GetDataForExcel(id);
StringBuilder sb = new StringBuilder();
sb.Append("<table border=`" + "1px" + "`b>");
sb.Append("<tr>");
// ... appending other strings and data
sb.Append("</table>");
// Return FileResult
byte[] byteArray = Encoding.UTF8.GetBytes(sb.ToString());
return File(new MemoryStream(byteArray, 0, byteArray.Length), "application/octet-stream", $"{firstTableObj.CurrentDate}.xlsx");
}
}
catch (Exception ex)
{
return Json(new RequestResult(RequestCode.Server_Failure, ex.Message), JsonRequestBehavior.AllowGet);
}
Currently, when I send a request from Angular 2+ frontend, the request is being processed and returned, this is some of the header data:
Content-Disposition:attachment; filename=12.11.2017.xlsx, Content-Length:617, Content-Type:application/octet-stream
However, the file is not being downloaded, the response body is just the html template in string format. What am I missing? I have seen some examples of returning excel files as a FileResult in MVC and they are not much different from mine. Changing the MIME to 'application/vnd.ms-excel' didn't help either. Also, as far as I am aware, there is no need to implement any file download logic in the client, it should work as is, that is a response from the server. Will appreciate any hints on the subject.
P.S: I know that in general I should not load an entire file into memory, but this is currently for testing purposes (I know an approximate limit to returned file sizes) and will most surely be changed in the future development.