1

I'm using 2.2.3 version of Play Framework, and my snippet for uploading file is as follows:

def process = SecuredAction(false, None, parse.temporaryFile) {
request =>

    val uploadedFile = request.body.file

    service.processFile(uploadedFile)    
    ...

 }

This parse.temporaryFile parser is polluting my temp folder with the files similar to this one: requestBody4950142040693742317asTemporaryFile_1424693062067.zip. I want to get rid of these files from my temp folder. Any suggestion how to prevent them from creation or how to delete them after uploading?

biesior
  • 55,576
  • 10
  • 125
  • 182
Filip
  • 2,244
  • 2
  • 21
  • 34

4 Answers4

1

I've never used Play's upload but it seems you can do:

service.processFile(uploadedFile)
request.body.clean()
Ok("All done!")

I'm assuming service.processFile is synchronous here.

Andreas Du Rietz
  • 1,151
  • 11
  • 16
1

@AndreasDuRietz is correct that you can call request.body.clean(), which is just an alias to delete the underlying File. However, this is not necessary. TemporaryFile overrides finalize, which will call clean() for you when the object is garbage collected.

See the source.

Michael Zajac
  • 55,144
  • 7
  • 113
  • 138
  • But keep in mind that `finalize` may never be invoked. [See why](http://stackoverflow.com/a/2506509/1059400) – HEX Sep 01 '15 at 10:43
0
                /*delete to-server-temp-folder uploaded file*/
                  request.body.file("Img").foreach {
                    case FilePart(_, _, _, ref, _, _) =>
                      val file = new File(ref.path.toString)
                      if (file.exists()) file.delete()
                  }
John
  • 2,633
  • 4
  • 19
  • 34
0

Play! 2.7.x

    request.body.delete()
Hahn
  • 367
  • 6
  • 6