0

I have an export button in my application. When I click the button, it posts back to the server and generates an Excel file.

I want to show a loading gif during the process, but when the open/save dialog shows, I want to hide the gif.

The problem is that I use Response.End to display the file open/save dialog.

Does anyone knows how can I call a client side function after Response.End, or anyone has another solution how can I hide the gif and let the user work?

here is a piece of my code:

     protected void Page_Load(object sender, EventArgs e)
    {
        //starts the loading gif animation.
        btnExport.Attributes.Add("onclick", "setTimeout(\"UpdateImg();\",300);");
    }

    protected void btnExport_Click(object sender, EventArgs e)
    {
        //generates the file
        Response.ContentType = "application/vnd.openxmlformats-  officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;  filename=ExcelDemo.xlsx");
        Response.BinaryWrite(pck.GetAsByteArray());
        Response.Flush();
        //Response.End();
        HttpContext.Current.ApplicationInstance.CompleteRequest(); 

        //HERE I WANT TO STOP THE ANIMATION AND GO BACK TO THE PAGE!
    }

Thank you, Inbal.

Inbal
  • 909
  • 2
  • 28
  • 46

1 Answers1

1

This way you can not do that.

When you start send your data to the file and open a save dialog, your web page have lost the control and you can not do anything any more, the data are all go to the file save.

What other and in my opinion better, options you have.

You can use a handler .ashx to create and send your file, and you give the link to your page with the parameters you need to make the file.

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • What is the difference between redirecting to a .aspx page and using .ashx page to generate and send the file? and how can I redirect back to my main page after this process completes? – Inbal Jun 11 '12 at 08:44
  • 1
    @Inbal when you use a handler and a redirect or a link to the file, you can continue run javascript and close your dialog - in this case you still have the control on the same page, the handler opens new different page that is not affect the first one. – Aristos Jun 11 '12 at 08:56
  • @Inbal read this answer to see http://stackoverflow.com/questions/10912164/what-is-the-best-way-to-download-file-from-server/10912955#10912955 – Aristos Jun 11 '12 at 08:57
  • Thank you very much for your answers and explanations. I Used iframe and got the wanted result. Now, my problem is that the generate and save process is done in a different page, so I don't know when to stop the loading animation (I wanna do it when the save popup is shown). Do you have any idea how should I know? – Inbal Jun 11 '12 at 11:22
  • @Inbal Finally, did you find a solution on this problem? I know that this post is 5 years old but I face the same issue. Please give me your feedback in order for me to avoid opening a new thread with the same subject. Also I would appreciate anyone else who could help. – gkoul Mar 25 '17 at 19:44