RegisterStartupScript after Response.WriteFile?

M

MJP

I have a button which kicks off the generation of a report after which
the file will be downloaded. The report generation can take a long
time, so client side onclick event of the button also makes a <span>
tag visible which contains a nice message to the user thanking them
for their patience. Of course after the report has been generated and
downloaded this message should be removed. However, the
RegisterStartupScript isn't working after calling Response.WriteFile.
Any alternative solutions/suggestions to solve this issue?

Code:

protected void btnExecute_Click(object sender, System.EventArgs e)
{

CreateReport();

Response.AppendHeader("content-disposition", "attachment;
filename=<filename>");
Response.WriteFile(<filename>);
Response.Flush();

Page.RegisterStartupScript("ToggleWait",
"<script>javascript:spnMessage.style.display = \"none\";</script>");
}
 
G

Guest

I have a button which kicks off the generation of a report after which
the file will be downloaded. The report generation can take a long
time, so client side onclick event of the button also makes a <span>
tag visible which contains a nice message to the user thanking them
for their patience. Of course after the report has been generated and
downloaded this message should be removed. However, the
RegisterStartupScript isn't working after calling Response.WriteFile.
Any alternative solutions/suggestions to solve this issue?

Code:

protected void btnExecute_Click(object sender, System.EventArgs e)
{

CreateReport();

Response.AppendHeader("content-disposition", "attachment;
filename=<filename>");
Response.WriteFile(<filename>);
Response.Flush();

Page.RegisterStartupScript("ToggleWait",
"<script>javascript:spnMessage.style.display = \"none\";</script>");



}- Hide quoted text -

- Show quoted text -

You can't register RegisterStartupScript, because you already sent a
file to HTTP response...

Remove the following code:

/*
Response.AppendHeader("content-disposition", "attachment;
filename=<filename>");
Response.WriteFile(<filename>);
Response.Flush();
*/

and modify your RegisterStartupScript as per

Page.RegisterStartupScript("ToggleWait",
"<script>javascript:spnMessage.style.display = \"none
\";window.open('<filename>');</script>");

It should hide the spnMessage span tag and open the file in a new
window. If you like to force a Save As window to be shown, you need to
have another page Download.aspx where you can have

Response.AppendHeader("content-disposition", "attachment;
filename=<filename>");
Response.WriteFile(<filename>);
Response.Flush();

Then open that page using window.open('Download.aspx?
filename=<filename>');

Should work, I think.
 
M

MJP

Thanks Alexey, that does work.

However, I of course now need to close the window that I open to run
Download.aspx after the file has been written to the HTTP response,
and since RegisterStartupScript won't work after writing the file, I
essentially still have the same problem....
 
G

Guest

Thanks Alexey, that does work.

However, I of course now need to close the window that I open to run
Download.aspx after the file has been written to the HTTP response,
and since RegisterStartupScript won't work after writing the file, I
essentially still have the same problem....


I think that when you have a content-disposition=attachment and user
has selected Save button, the popup window will be closed
automatically.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top