views:

33

answers:

1

I have a jQuery plugin I use to dynamically create and render a form on a default.aspx asp.net page, then submit it. The page it gets submitted to is a pdf.aspx page. The page builds a PDF then uses Response.Write to write the file (application/pdf) to the browser. I use the same method to render XLSX files to the browser as well. It works really great, but I need a callback or some event to tell the button when to stop spinning. This prevents the user from continuously clicking the Excel or PDF buttons. Does anyone know a way to detect the file dialog window when it was not created using javascript? I am also open to other methods of callback from the server side as well.

A: 

The way I do that was suggested in response to a question I asked here a while ago by T.J. Crowder. I can't find the response from the last time I wrote this up because the Stackoverflow "search" facility is so incredibly lame, so I'll probably type in a blog post. The basic idea is that your client code (Javascript) should append an extra parameter when it submits the request for the download. The parameter should contain some generated random string (probably just the current timestamp is good enough). The server then looks for that parameter, and when it's preparing the response with the download file it also sets a cookie and gives it that random value.

Right after the submit (or right before; it doesn't really matter), the Javascript code should start an interval timer with a routine to look at the value of document.cookie and see if it contains that random string. As soon as the cookie does contain that string, then you know that the server has sent back its response and that the file download dialog has been presented.

Pointy
That is funny. I tried that exact solution yesterday, as that was the only ones I came up with. Once setting the cookie on the server side, I poled on the client side looking for it using: setTimeout(complete = checkCookie("fileComplete"), 2000); every 2 seconds. This did not work because I could not read the cookie client side. Perhaps I will dig deeper into that idea and try it again today. If it works, I will let you know.
Zacho
Well if you make up a random string on the client, you don't really even need to look for a particular cookie - you can just look at "document.cookie". Also, make sure that your server is not making your cookies be "http-only" or else your script can't see them! This technique works like a champ for me.
Pointy