0

In my application i have to export the excel file which I have achieved by below code:

            Response.ClearContent();
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", pFileName));
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

            FileInfo myFile = new FileInfo(fullfilePath);
            Response.WriteFile(myFile.FullName);
            Response.Flush();
            HttpContext.Current.ApplicationInstance.CompleteRequest();

            Response.End();

From my main page where "Export To Excel" button is present, on click of of that button I have registerred a Javascript as below:

            ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "<script language=JavaScript>window.location.href('GenerateExcel.aspx');</script>");

and on the page load of GenerateExcel.aspx page i have written the code for exporting file that I posted above. This code is work for me, when users hits the "Export To Excel" button windows file Open , save & close popup is apeear and user is able to save or open the file but my problem here is that after that file open popup I want to refresh the main page. I have tried registering javascript but after Response.End nothiing is working. Please help me.

Aayush Kumar
  • 1,618
  • 1
  • 11
  • 31
Rohit Vyas
  • 1,949
  • 3
  • 19
  • 28
  • 1
    its better to make a handler to send the file using a simple link and not a post back, and then you can make what other thniks you like. – Aristos May 07 '12 at 11:36

1 Answers1

0

You have changed the location of the main window with window.location.href and then the HTTP response has the Excel MIME type. The main window no longer has an HTTP response to work with and is unable to refresh.

Instead you should open a completely new window and use that to send the Excel file response. You will then still have the main window available to refresh.

Your script should be something like;

<script> 
  window.open('GenerateExcel.aspx', 'Export To Excel');
  window.location.href=window.location.href;
</script>

Another way to avoid opening a window is to make the HTTP Request with JavaScript but that is going to need jQuery for a reliable cross browser solution.

Community
  • 1
  • 1
Dave Anderson
  • 11,836
  • 3
  • 58
  • 79