Download workbook from web server with page having Ajax call using Aspose.Cells for .NET eith C#

Hi guys,

I've used the example code to import the data from a gridview control into an Excel spreadsheet and want to serve the newly-created workbook up to the user to save somewhere but it isn't working.

Here's the code I'm using:

//Instantiate a new workbook

Workbook workbook = new Workbook();

//Get the first worksheet in the workbook

Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0];

//Import data from GridView control to fill the worksheet

worksheet.Cells.ImportGridView(grdDispatched, 0, 0, false, true, false);

//workbook.Save("c:\\PrintRoomDispatchSummary.xls");

//Save with default format, send the file to user so that he may open the file in

//some application or save it to some location

workbook.Save(this.Response, "PrintRoomDispatchSummary.xls", ContentDisposition.Attachment, new XlsSaveOptions());

The code crashes Internet Explorer, and Firefox is more resilient, but neither prompts the user to save the file.

If I comment out the save and reinstate the save to disk it works fine, but I really need it to serve up the file.

Is there something I need to do to get this working or have I done something wrong?

Thanks in advance,

Paul.

Hi,


I think it is not an issue with Aspose.Cells as it saves the streams to the disk fine. For your information, the line of code, i.e…,

workbook.Save(this.Response, “PrintRoomDispatchSummary.xls”, ContentDisposition.Attachment, new XlsSaveOptions());

is just equivalent to:


MemoryStream ms = new MemoryStream();
workbook.Save(ms, SaveFormat.Excel97To2003);
this.Response.ContentType = “application/vnd.ms-excel”;
this.Response.AddHeader(“content-disposition”, “attachment; filename=PrintRoomDispatchSummary.xls”);
this.Response.BinaryWrite(ms.ToArray());

you may confirm the same behavior without involving Aspose.Cells’ Workbook.Save() method.

I think the issue might be with your system configuration or settings for the browser types etc., see the document for your reference:
http://9to5it.com/internet-explorer-disable-do-you-want-to-open-or-save-this-file-prompt/

Thank you.

Hi Amjad,

I’ve found the cause of the issue, and it appears to be Ajax.

It would seem that neither of the above methods will work on a page that has Ajax enabled on it.

To resolve this issue, I have:

  • Created a new, plain Web Form (no master page, no Ajax) called “download.aspx”.
  • Placed all of the above code into the Page_Load event of download.aspx.
  • Added the necessary code to pass the requisite data across using session variables.
  • Used the button on the existing page to invoke the new download.aspx.
The result is a prompt asking to save or download the document, which is exactly what I was after.

Many thanks for your help.

Regards,
Paul.

Hi,


Good to know that your have sorted it out. Feel free to contact us any time if you need further help or have some other queries or issue, we will be happy to assist you soon.

Thank you.