How to catch client disconnection when sending a large file?

23 views
Skip to first unread message

setu basak

unread,
Jul 23, 2017, 2:48:09 AM7/23/17
to Play Framework
I am sending a large file in play application response. When the client downloads the file, i am cleaning the file from the local server. I am achieving this using below code:

val fileToServe = TemporaryFile(new File(fileName))
Ok.sendFile(fileToServe.file, onClose = () => { fileToServe.clean })

But when client disconnects the connection, the temporary file remains in the local server. But i want to handle this disconnection and clean up the
temporary file. I heard about onDoneEnumerating() but couldn't use it.

Can anyone point me out the easiest way to catch the disconnection and clean up the temporary file from local server?

Will Sargent

unread,
Jul 23, 2017, 8:50:55 PM7/23/17
to play-fr...@googlegroups.com
TemporaryFile is for when Play receives a data stream that has to be kept in a temporary file location, rather than you sending a file out.  It removes on finalization (pre 2.6) or on phantom file reference (2.6.x).  

The easiest way to catch the disconnection is to call Files.deleteIfExists


in the onClose block.  If that doesn't seem to be working for some reason, you can use the temporary file reaper:


that will clean out the temp files directory every so often.

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/dcaa752d-7eb1-4cc0-83d9-cfa47f8b035d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages