2
Situation: I have an ASP MVC5 API that generates an excel.xlsx and returns it in a Filecontentresult. All in memory, because I can’t save the file to the server disk.It works ok if I access the url directly.
I have an Angularjs application that needs to pass a huge Json to the API and receive the generated file . xlsx.
I’m trying the following:
Controler:
public async Task<FileContentResult> Excel([FromBody]GetGeneralFilterVM operationHistoryFilter = null)
{
var ListaOperazioni = await GetListaOperazioniData(operationHistoryFilter);
var Totals = await GetExcelTotalsData(operationHistoryFilter);
var excelExport = new ExcelExportEntity(new object[]
{
ListaOperazioni,
Totals,
});
var preFile = excelExport.DoExcel();
var arraybits = preFile;
var file = File(arraybits, "application/vnd.ms-excel", "OperationHistory.xlsx");
return file;
}
Angular:
$scope.exportExcel = () => {
$.ajax({
cache: false,
url: appPath + "controller/Excel",
data: filter,
success: function (response) {
var file = new Blob([response], { type: "application/vnd.ms-excel" });
var fileName = "excelFeliz.xlsx";
saveAs(file, fileName);
},
error: function (ajaxContext) {
alert('Export error: ' + ajaxContext.responseText);
}
});
}
Upshot: Thus it even reaches to download a file, however while trying to open it is corrupted.
My insistence on AJAX is because of the Getgeneralfiltervm I’m receiving in the controller, it contains sub-objects with many properties it would be complicated to put this as parameters in the url.
Also I have no way to generate and return a url to download, because I have no way to save the file to the server disk.
Any idea?
Eia Alam - how 'huge' is this file? If you are doing everything in memory you are probably limited to a usage quota.
– OnoSendai
Excel itself is not so big, it gets about 10 or 15KB. Why the question?
– Alam Baier
@Onosendai actually what I said is "huge" is Json with the parameters to filter the data. He is not great in weight, but in number of fields. The Getgeneralfiltervm (forget this get in front, the bad name ta) is a viewmodel with 5 objects and each object with its properties... added are more than 30 and some are lists
– Alam Baier