pat.inside
pat.inside

Reputation: 69

How can I know when the IE file download prompt pop up?

Now i have a problem below:

i've added a div on my page to forbid user to click buttons, links or fields when user click the DOWNLOAD button. So i need to remove this div when the IE file download prompt pops up or when user clicks "save", "save as" or "cancel" on it.

How can I reach this?

IE only considered please.

Upvotes: 1

Views: 1728

Answers (3)

Pointy
Pointy

Reputation: 413996

There actually is a way that you can make a good guess that the browser is handling a file download. I asked this same question and TJ Crowder had a great idea:

What are techniques to get around the IE file download security rules?

I'm actually using the idea now and it works perfectly. The trick is to have the page send a "nonce" parameter back with a random string of characters in it. The page then starts polling document.cookie every 100 milliseconds or so, checking to see whether that string of characters is in the cookie yet.

The server, in turn, sets a chosen cookie (doesn't really matter what it's called) to the value sent by the form in the "nonce" parameter. It then ships out the file download as it normally would.

When the HTTP response gets back to the browser, the cookie will be set. The Javascript that's polling for the cookie value will see that, and it will then know that the HTTP response is being processed. Now, it won't know of course that the user didn't hit "Cancel" on the file download.

If the server decides that the original request is in error (say, if the file download involves a form and the user supplied bad input or missed a field), then it won't set the cookie and can just respond with HTML for the error (or whatever).

Upvotes: 5

Colin Pickard
Colin Pickard

Reputation: 46673

No, the browser file handling dialogs can't be detected from javascript - this is by design. Even if you limit yourself to IE, it is not possible, and other browsers handle file downloads in completely different ways anyway.

http://groups.google.co.uk/group/comp.lang.javascript/browse_thread/thread/9441cce4f8fdf20b/6f4fcc7796e03f1d

If you explain why you're trying to do this, there may be a different approach which suits you better.

Upvotes: 3

Piskvor left the building
Piskvor left the building

Reputation: 92792

I don't think there's a callback for that; definitely not in all browsers.

Also, not every browser pops up a prompt - and that depends on user-specific browser configuration; some may just start downloading in the background.

I'd suggest to remove the DIV after some interval (which you'll have to guess depending on the time it usually takes to show the download box), although it's not a very clean workaround.

Upvotes: 1

Related Questions