Rawling
Rawling

Reputation: 50114

After disabling a Submit button with JS, re-enable after file download

I'm working on an ASP.NET application. Currently when the user clicks "Save" on an item, we disable the Save button, change its text to "Saving..." and so on. This works fine, because the button causes a postback which results in a new page being loaded, on which the Save button is no longer disabled.

We now want to apply this to other actions: Publish (not much different from Save), Import (again, much like Save, but based on data the user has uploaded in a previous step), Export (downloads an XML file to the user) etc.

Export is causing me problems - the button stays disabled. I think this is because the server sends back an XML file rather than a new web page, and so the browser just displays the same page.

The server-side code is along the lines of

Response.Clear();
Response.BufferOutput = true;
Response.ContentType = "text/xml";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + whatever);
[push file contents into Response.OutputStream]
Response.End();

[No idea if this is good code or not - it's not mine - but it does the job :)]

Basically, I'd like to know either:

Upvotes: 2

Views: 2235

Answers (3)

Sandeep Choudhary
Sandeep Choudhary

Reputation: 23

There is a dirty hack. You can use setTimeOut method to enable and change back button caption/image.

So you can write a server side code similar to

btn.Attributes.Add("onclick", "this.disabled='true'; this.value='Processing...';_doPostback(this,null);setTimeout(function() { enable button logic....set the text /img of the button}, );");

The server side download attachment event will not be synch perfectly, but you can set timeout as 2-5 seconds depending on your server configuration.

Thank you!

Upvotes: 0

Rawling
Rawling

Reputation: 50114

Looks like this should do it: essentially, set a cookie with the file response, and have the browser waiting for that cookie in order to unblock the page.

Upvotes: 1

David Mårtensson
David Mårtensson

Reputation: 7600

The problem probably is that you do not load a new page at all.

Since content disposittion is attachement, the browser will not reload the page but only save the return from the server to disk.

You need to reload the page some how but I have no really good idéa on how to do that after a file fetch.

Upvotes: 0

Related Questions