aspose

How to save files generated using Aspose with a browser?


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!!


Solution

  • You may try either of the (below) code snippets:

    Workbook workbook = new Workbook();
    
    int index = workbook.Worksheets.Add();
    Worksheet worksheet = workbook.Worksheets[index];
    
    worksheet.Name = "EMP_USER";
    
    // Generate the content...
    ...
    ...
    
    //Send workbook to response
    workbook.Save(HttpContext.Current.Response, "EMP_USER_COPY.xlsx", ContentDisposition.Attachment, new OoxmlSaveOptions(SaveFormat.Xlsx));
    HttpContext.Current.Response.End();
    

    OR

    Workbook workbook = new Workbook();
    
    int index = workbook.Worksheets.Add();
    Worksheet worksheet = workbook.Worksheets[index];
    
    worksheet.Name = "EMP_USER";
    
    // Generate the content...
    ...
    ...
    
    //Send workbook to response
    OoxmlSaveOptions xSaveOptions = new OoxmlSaveOptions(SaveFormat.Xlsx);
    
    MemoryStream ms = new MemoryStream();
    workbook.Save(ms, xSaveOptions);
    
    //set the position.
    ms.Position = 0;
    
    this.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    this.Current.Response.AddHeader("content-disposition", "attachment; filename=EMP_USER_COPY.xlsx");
    Response.BinaryWrite(ms.ToArray());
    Response.End();
    

    See the document for your further reference. Let us know if you still find any issue.

    PS. I am working as Support developer/ Evangelist at Aspose.