so recently I’m working on the Aspose and encounter the problem.
I was using .NET Framework 4.8 & Aspose.Cells 24.4.0
I’m able to save the file directly in my local drive, but can’t save by browser.
I’m wondering how to fix it, and do I need to do something in my front-end ?
Below is my code:
I was using AJAX to post formdata so that can get the parameter I need.
**.cshtml
**
@section forms {
@using (Ajax.BeginForm("Create", "UserExport", null, new AjaxOptions
{
OnSuccess = "alertSuccess"
}, new { id = "frmExport" }))
...
...
}
Call function in controller
UserExportController.cs
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult Create(UserExportModel form, IEnumerable<HttpPostedFileBase> uploader)
{
using (UserExportDac dac = new UserExportDac(GetLoginUser()))
{
IList<UserModel> userList = dac.ReadAll();
if (form.isExcel == "Y"){
dac.ExportExcel(userList);
}
...
...
return Json(new { message = "Generatedd file successfly!" });
}
}
Using Aspose
UserExportDac.cs
public void ExportExcel(IList<UserModel> userList)
{
Workbook workbook = new Workbook();
int index = workbook.Worksheets.Add();
Worksheet worksheet = workbook.Worksheets[index];
worksheet.Name = "EMP_USER";
// Generate the content...
...
...
**// This work when I try saving in my local downloads**
// string filePath = System.Environment.ExpandEnvironmentVariables("%userprofile%/downloads/") + "EMP_USER_COPY.xls";
// workbook.Save(filePath);
// Below sample is query from the web, but didn't work me
MemoryStream memoryStream = new MemoryStream();
byte[] bytes = ms.ToArray();
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new ByteArrayContent(bytes);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "EMP_USER_COPY.xlsx";
response.Content.Headers.ContentType = new MediaTypeHeaderValue("Application/x-msexcel");
workbook.Save(HttpContext.Current.Response, "EMP_USER_COPY.xlsx", Aspose.Cells.ContentDisposition.Attachment, new OoxmlSaveOptions(Aspose.Cells.SaveFormat.Xlsx));
HttpContext.Current.Response.End();
}
So currently I was stuck in the saving step.
Any advice would be appreciate it, thanks!!
user24596335 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.