Send a workbook from an ajax call

How do I send a workbook to the user from an ajax call?


$.ajax({
url: “/Forms/Save2Excel”,
type: “POST”,
dataType: “json”,
data: ({ jsonData: jsonkdView, fileName: gridId }),
success: function (data) {
alert(data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText);
}
});

I can make the call to the server sending json, which is consumed on the server into a workbook, then i send that workbook back to the user. This all works just fine, but I do not get the ‘Save to Excel’ prompt.
BTW when i make the same server call from the browser command line (so not via an ajax call), I get the message, so I know the issue is with the ajax call.

So how do i do this? … How do i send a workbook to the user from an ajax call?

Rob

Hi,

Thanks for your posting and using Aspose.Cells for GridWeb.

Please download and use the latest version: Aspose.Cells
for GridWeb v2.7.7.2002
and let us know your feedback.

If your problem still occurs, please provide us a sample runnable project replicating this problem of sending workbook from ajax call alongwith the source xls/xlsx file being used by you.

Please also provide the screenshot to elaborate this issue more, it will help us investigate this issue.

Thanks for the quick reply.


Here is my jquery.ajax call to the server …

$.ajax({
url: “/Forms/Save2Excel”,
type: “POST”,
dataType: “json”,
data: ({ jsonData: jsonkdView, fileName: gridId })
});

… and on the server I’m using asp.net mvc 3 calling into the below method …

[HttpPost]
public ActionResult Save2Excel(string jsonData, string fileName)
{

JavaScriptSerializer jss = new JavaScriptSerializer();
dynamic dynamicData = jss.Deserialize(jsonData);

// create a data table from the json data
System.Data.DataTable dataTable = new DataTable();
System.Data.DataColumn dataColumn;
System.Data.DataRow dataRow;
foreach(string key in dynamicData[0].Keys)
{
dataColumn = new DataColumn();
dataColumn.DataType = System.Type.GetType(“System.String”);
dataColumn.ColumnName = key;
dataTable.Columns.Add(dataColumn);
}

// fire the data into this table
string value = string.Empty;
foreach(Dictionary<string,object> item in dynamicData)
{

dataRow = dataTable.NewRow();
foreach(string key in item.Keys)
{
value = item[key].ToString();
dataRow[key] = value;
}
dataTable.Rows.Add(dataRow);
}

Aspose.Cells.License license = new Aspose.Cells.License(“Aspose.Total.lic”);
Workbook workbook = new Workbook();
workbook.Worksheets.Add();
workbook.Worksheets[0].Cells.ImportDataTable(
dataTable , true, “A1”);

HttpContext context = System.Web.HttpContext.Current;
workbook.Save(context.Response, fileName + “.xls”,Aspose.Cells.ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Excel97To2003));

return null ;

}

This code works … it sends the data back to the browser … but it does not initiate the ‘File Save As’ popup asking if the user wants to open the file up in excel … thats what I cannot get to work …

Any suggestions would be most appreciated.

Rob



Hi,

Thanks for your sample code.

Your code looks fine to me except that you are not ending the response stream, so please change your code into this and see if it fixes your problem. Your code will looks like the following

C#
HttpContext context = System.Web.HttpContext.Current;
workbook.Save(context.Response, fileName + “.xls”,Aspose.Cells.ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Excel97To2003));
HttpContext.Current.Response.End();
return null ;

Another thing is that you could try is ContentDisposition.Inline instead of ContentDisposition.Attachment.

a) Please call Response.End after calling workbook.Save method:

C#

wb.Save(this.Response, “AlertSummary.xls”, Aspose.Cells.ContentDisposition.Inline, new Aspose.Cells.XlsSaveOptions(Aspose.Cells.SaveFormat.Excel97To2003));

Response.End();



b) If Response.End() still does not workbook, please save the workbook to stream, then export it by Response, see the following code:

C#
MemoryStream ms = new MemoryStream();
wb.Save(ms, new Aspose.Cells.XlsSaveOptions(Aspose.Cells.SaveFormat.Excel97To2003)); ;


this.Response.ContentType = “application/vnd.ms-excel”;

Response.AddHeader(“content-disposition”, “inline; filename=AlertSummary.xls”);

Response.BinaryWrite(ms.ToArray());


Response.End();

Hi,

I am having the exact same issue, but using Java. I was hoping you could help me.

My code looks like this:

String contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
resp.setContentType(contentType);
resp.addHeader("content-disposition", "attachment;filename="+fileName);
workbook.save(resp.getOutputStream(), SaveFormat.XLSX);String contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
resp.setContentType(contentType);
resp.addHeader("content-disposition", "attachment;filename="+fileName);
workbook.save(resp.getOutputStream(), SaveFormat.XLSX);

If I go to this routine using a form submit it works fine, but when I use a JSON post, the workbook gets filled up fine but won't call the "save or open" dialog box on the page.

Thanks,

Brita

Hi Brita,

Thanks for your posting and using Aspose.Cells for Java.

Your code looks fine and there seems to be no problem. However, you can try inline instead of attachment.

So, please change the following line

resp.addHeader(“content-disposition”, “attachment;filename=”+fileName);

into this line

resp.addHeader(“content-disposition”, “inline;filename=”+fileName);

and see how things go at your end.