Hi
I am trying to create an Excel file after an Ajax post.
I use the Hander method with Razor pages.
The file is created server side. After that the file should be opened as stream as ajax result. I don't want to write the file to the filesystem.
I used this example as a template and converted it to the handler method.
https://ej2.syncfusion.com/aspnetcore/Excel/Create
In this example, the file is also automatically opened with the correct content type, and no javascript is required to open the file.
In my example the file is not opened automatically and I have to open it with javascript, which I don't like either.
The opened file is then corrupt.
I have tried quite a few hours and many examples. Unfortunately I did not reach the goal.
Thanks for the help
Reto
View
@page "{handler?}"
@model Razor_SyncMix.Pages.ExcelModel
@using Syncfusion.EJ2
@{
ViewData["Title"] = "Excel";
}
<input id="createbtn" class="btn btn-primary e-btn" type="button" onclick="createExcel()" name="button" value="Create Document" style="width:160px; text-transform:capitalize" />
<script>
// create Excel
createExcel = function() {
let ajax = new ej.base.Ajax({
url: "?handler=Create", processData: false, dataType: "Application/vnd.ms-excel", type: "POST"
});
ajax.send().then();
ajax.onError = function(data) {
alert("Error: handler=Excel")
}
ajax.onSuccess = function(data) {
var blob = new Blob([data], { type: "Application/vnd.ms-excel" })
var url = window.URL.createObjectURL(blob);
var a = document.createElement("a");
document.body.appendChild(a);
a.rel='nofollow' href = url;
a.click();
};
}
</script>Controller
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Syncfusion.XlsIO;
namespace ExcelTest.Pages
{
[IgnoreAntiforgeryToken] // Ajax Post
public class ExcelModel : PageModel
{
public ActionResult OnPostCreate(string SaveOption)
{
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
// Workbench
IWorkbook workbook = application.Workbooks.Create(1);
workbook.Worksheets[0]["A1"].Text = "Ajax Call";
//Set the version of the workbook
//workbook.Version = ExcelVersion.Excel2013;
var ContentType = "Application/msexcel";
//Save the workbook to stream
MemoryStream outputStream = new MemoryStream();
workbook.SaveAs(outputStream);
outputStream.Position = 0;
var fileName = "text.xls";
return File(outputStream, ContentType, fileName);
}
}
}
Hi Reto,
Greetings from Syncfusion.
We request you to follow the steps documented in below KB to create Excel document with Ajax call without any corruption.
https://www.syncfusion.com/kb/10730/download-excel-from-ajax-call-in-asp-net-core
If you still face any issue, kindly share the issue reproducing sample and corrupted Excel document, which will be helpful for us in investigating the query further.
Regards,
Keerthi.
Hi Keerthi
Thank you very much for your feedback.
After a few more hours of research, I found a simple solution a few minutes ago.
With Ajax and FileStreamResult apparently everyone has problems.
I have now 2 simple solutions.
Change the Controller to the GET method. > OnGetCreate
1st solution without javascript: use asp-page-hander,
<a class="btn btn-success" asp-page-handler="Create">Download Excel</a>
2. solution using javascript:
window.open("Excel/Create", '_blank');Regards, Reto
Hi Reto,
We are glad that you have found solution and resolved the queries. Kindly let us know if you need any further assistance.
Regards,
Keerthi.