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
TemporaryFilecase class. ThemoveTomethod can have 2 parameters; the second one defaults tofalseand indicates that you want to replace an existing file.So, in short, instead of
you write