Hi,

We had a similar problem. We just added a "preparation" step before the actual 
download. 

1. User clicks on "request download" link 
2. jQuery sends a request to servlet and instructs it to prepare the download 
3. Meanwhile the request download link has been changed with Javascript to 
"preparing download..." 
4. jQuery periodically asks the servlet if the download is ready or if the 
preparation has failed
5. If it is ready, the "preparing download..." is replaced by "download file" - 
if it has failed, an error message would be displayed

This of course will only work if the client supports Javascript. But even if it 
doesn't you can work with HTTP reloads and/or redirects and using unique IDs to 
identify your client and their download.

Best Refards
Sebastian Trost

-----Original Message-----
From: Steffen Heil (Mailinglisten) [mailto:li...@steffen-heil.de] 
Sent: Sunday, May 29, 2016 8:08 PM
To: Tomcat Users List <users@tomcat.apache.org>
Subject: How to cancel download on the server side

Hi


I am streaming a huge file from a servlet to the browser.
It can easily be multiple gigabytes.

Currently the data is prepared on the server, stored in a file and then sent to 
the client with a "Content-Disposition: attachment" header, so the browser 
handles it as a download. After the transfer the file is immediately deleted.

This kind of works but has two big disadvantages:
1. The client has to wait a long time until the first byte is transferred. I am 
afraid I could run into browser (or generic client) timeouts.
2. I need a lot of storage on the server.

The data I have could easily be streamed directly to the client without storing 
it on the server at all.
I would not know the precise size of the data In advance, but it could be 
transferred using "Transfer-Encoding: junked" so this would not be a problem.

My problem is that if anything goes wrong while creating the data I have no way 
to notify the client, as the response headers were already sent way before.
So I am looking for a way to cancel the download from the server side and 
letting the client know that something went wrong.
Simply stopping sending data is not enough, the client needs to know that the 
data is incomplete. 

Probably the only way to do that would be to abruptly disconnect the http(s) 
connection without completing the download using a "0\r\n" end marker.

So my questions are:
1. How can I force tomcat to disconnect a client like that?
2. Does anyone here have tried anything like that before? What client side 
reactions did you notice?


Best regards,
  Steffen


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to