Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax file upload with Play 2.0: exists but replace parameter is false

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?

like image 927
Otavio Macedo Avatar asked Nov 12 '12 12:11

Otavio Macedo


2 Answers

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)
like image 96
Carsten Avatar answered Nov 05 '22 14:11

Carsten


From the moveTo() API doc, to replace the file, you should use:

request.body.moveTo(new File("/somepath/foo.jpg"), true)
like image 31
ndeverge Avatar answered Nov 05 '22 13:11

ndeverge