views:

22

answers:

2

Hi all,

How do you force a download prompt to popup before you send any data to browser? I know about content disposition attachment, but this is different. Basically, the servlet starts sending data to the client, and then the client open a dialog, open, save, cancel.

The probably is my servlet is slow getting the data, and it gets all the data in memoery before it sends any thing to the client. I would like to do something to trigger the dialog, before I am ready to send the data. Otherwise, the browser just waits there, like you did nothing.

I want to trigger that save dialog sooner. I can't send data, because the data is not ready.

Any ideas?

Grae

A: 

You can simply use the OutputStream and send data over it. This way you don't have to get the whole data in memory.


On the same topic :

Colin Hebert
The thing is, I want to trigger the save as dialog, before I am ready to send the data. Is there a way to start the dialog, before the first OutputSteam.println()
Grae
You can send useless data, but the thing is if you just want to upload something, you don't have to load it in memory first.
Colin Hebert
"Useless data" may harm, especially when the whole response concerns binary data.
BalusC
@BalusC "\0" usually don't harm, but yes, it's still a bad idea. (And you can forget hashes on your file), that's why I insist on the good way to do this.
Colin Hebert
I wouldn't mind send useless data in a way, but it would corrupt the file the user is downloading.
Grae
+1  A: 

If you want your app to appear more responsive to the user (and prevent multiple clicks on the download link) consider to make the link point to a location (servlet or JSP) which does nothing but issue a 301/302 redirect with a reponse body, i.e. an HTML page displaying a message asking the user to be patient while the data is collected. The redirect's location should then point to your servlet which delivers the download.

Thomas Weber