1

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

GC_
  • 1,673
  • 6
  • 23
  • 39
  • Where does this data come from? Slowness is to be solved by just **immediately** writing the incoming bytes to the output rather than getting hold of it in Java's memory first. [Here is another example handling a CSV case](http://stackoverflow.com/questions/3458577/accessing-data-from-servlet/3458921#3458921). – BalusC Sep 09 '10 at 19:31
  • The slowness is probably difficult to get rid of. – GC_ Sep 09 '10 at 19:44

2 Answers2

1

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
  • 1,005
  • 7
  • 17
0

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 :

Community
  • 1
  • 1
Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
  • 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() – GC_ Sep 09 '10 at 19:18
  • 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 Sep 09 '10 at 19:19
  • "Useless data" may harm, especially when the whole response concerns binary data. – BalusC Sep 09 '10 at 19:31
  • @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 Sep 09 '10 at 19:32
  • I wouldn't mind send useless data in a way, but it would corrupt the file the user is downloading. – GC_ Sep 09 '10 at 19:47