Search code examples
scalafile-uploadvuejs2playframework

Play! FrameWork: I'm having a problem uploading an image to my server


i hope someone can help me. I use following Code snippet to upload an image to my server (This is copied from here: https://www.playframework.com/documentation/2.8.x/ScalaFileUpload

```def upload = Action(parse.multipartFormData) { request =>
    request.body
        .file("file")
        .map { picture =>
        
        val filename    = Paths.get(picture.filename).getFileName
        val fileSize    = picture.fileSize
        val contentType = picture.contentType

        // Test Print
        print("content Type: "+contentType)
        print("filename: "+filename)
        print("filesize: "+fileSize)

        picture.ref.copyTo(Paths.get(s"/tmp/picture/$filename"), replace = true)
        Ok("File uploaded")
        }
        .getOrElse {
        null //TODO: changeme
        }
    }```

I checked filename, filesize and content-type with my print methods and it seems to be working..

But i always get the following exception:

java.lang.ClassCastException: xsbti.BasicVirtualFileRef cannot be cast to java.io.File
        at play.sbt.run.PlayReload$.$anonfun$sourceMap$1(PlayReload.scala:70)
        at scala.collection.MapLike$MappedValues.get(MapLike.scala:261)
        at play.runsupport.Reloader.$anonfun$findSource$1(Reloader.scala:552)
        at scala.Option.flatMap(Option.scala:271)
        at play.runsupport.Reloader.findSource(Reloader.scala:551)
        at play.core.server.DevServerStart$$anon$1$$anon$2.sourceOf(DevServerStart.scala:174)
        at play.core.SourceMapper.$anonfun$sourceFor$1(ApplicationProvider.scala:22)
        at play.core.SourceMapper.$anonfun$sourceFor$1$adapted(ApplicationProvider.scala:22)
        at scala.collection.IndexedSeqOptimized.$anonfun$find$1(IndexedSeqOptimized.scala:53)
        at scala.collection.IndexedSeqOptimized.$anonfun$find$1$adapted(IndexedSeqOptimized.scala:53)

Some hard facts:

  • Fronted: Vue.js 2.6.12
  • Backend: Play!Framework with Scala (2.12.8) and sbt version 4.0.0
  • This is my inputfield in vue: <input type="file" ref="file" @change="onSelect" />

I really hope someone can help me:). If u need more information to help, just ask me:)

Thank you!


Solution

  • According to this thread, Play may have problems with certain SBT versions and vice versa.

    This workaround might work for you.

    This issue was also patched for Play 2.8.8 so try updating to latest version.