I'm implementing Ajax file upload with Valum on the client side and Play! 2.0.4 on the server side. My action method looks like this:
def ajaxup = Action(parse.temporaryFile) { request =>
try {
request.body.moveTo(new File("/somepath/foo.jpg"))
} catch {
case e: Exception => Logger.error(e.getMessage)
}
Ok("File uploaded")
}
The upload works fine, that is, the file is correctly saved under the name foo.jpg
on /somepath/
. But it also throws the exception:
Path(/somepath/foo.jpg) exists but replace parameter is false
How can I avoid that? Do I need set any property on the file I'm creating?
I assume that you want to overwrite the existing file /somepath/foo.jpg
.
In case you don't, just check before if that file already exists.
Anyway, the error message already gave you a hint.
Look at the documentation for the TemporaryFile
case class. The moveTo
method can have 2 parameters; the second one defaults to false
and indicates that you want to replace an existing file.
So, in short, instead of
request.body.moveTo(new File("/somepath/foo.jpg"))
you write
request.body.moveTo(new File("/somepath/foo.jpg"), true)
From the moveTo() API doc, to replace the file, you should use:
request.body.moveTo(new File("/somepath/foo.jpg"), true)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With