title: How to upload an image with Spray?
body: <p>How do I handle uploads of images with Spray? A simple example would suffice. Currently, I'm trying to follow the <a href="https://github.com/eigengo/activator-akka-spray#master" rel="nofollow">tutorial</a> bundled with Typesafe Activator on Spray + Akka, but I get an error when I upgrade to the latest spray version. </p>
  
  <p>In particular, the line 42:</p>
  
  <p><code>data.fields.get("files[]") match {</code></p>
  
  <p>complains that it "cannot resolve symbol get".</p>
  
  <p>The full source for <code>RegistrationService.scala</code> follows, but you can also find the full source at the github repo here: <a href="https://github.com/eigengo/activator-akka-spray/blob/master/src/main/scala/api/RegistrationService.scala" rel="nofollow">https://github.com/eigengo/activator-akka-spray/blob/master/src/main/scala/api/RegistrationService.scala</a></p>
  
  <pre><code>package api
  
  import spray.routing.Directives
  import scala.concurrent.ExecutionContext
  import akka.actor.ActorRef
  import core.{User, RegistrationActor}
  import akka.util.Timeout
  import RegistrationActor._
  import spray.http._
  import core.User
  import core.RegistrationActor.Register
  import scala.Some
  
  class RegistrationService(registration: ActorRef)(implicit executionContext: ExecutionContext)
    extends Directives with DefaultJsonFormats {
  
    case class ImageUploaded(size: Int)
  
    import akka.pattern.ask
    import scala.concurrent.duration._
    implicit val timeout = Timeout(2.seconds)
  
    implicit val userFormat = jsonFormat4(User)
    implicit val registerFormat = jsonFormat1(Register)
    implicit val registeredFormat = jsonObjectFormat[Registered.type]
    implicit val notRegisteredFormat = jsonObjectFormat[NotRegistered.type]
    implicit val imageUploadedFormat = jsonFormat1(ImageUploaded)
  
    implicit object EitherErrorSelector extends ErrorSelector[NotRegistered.type] {
      def apply(v: NotRegistered.type): StatusCode = StatusCodes.BadRequest
    }
  
    val route =
      path("register") {
        post {
          handleWith { ru: Register =&gt; (registration ? ru).mapTo[Either[NotRegistered.type, Registered.type]] }
        }
      } ~
      path("register" / "image") {
        post {
          handleWith { data: MultipartFormData =&gt;
            data.fields.get("files[]") match {
              case Some(imageEntity) =&gt;
                val size = imageEntity.entity.buffer.length
                println(s"Uploaded $size")
                ImageUploaded(size)
              case None =&gt;
                println("No files")
                ImageUploaded(0)
            }
          }
        }
      }
  
  }
  </code></pre>
  
  <p>For completeness, here is my build file:</p>
  
  <pre><code>name := """activator-akka-spray"""
  
  version := "1.0"
  
  scalaVersion := "2.11.2"
  
  resolvers += "spray repo" at "http://repo.spray.io"
  
  resolvers += "spray nightlies" at "http://nightlies.spray.io"
  
  libraryDependencies ++= {
      val sprayV = "1.3.2"
      val akkaV = "2.3.6"
      val sprayJsonV = "1.3.1"
      val logbackV = "1.1.2"
      val specsV = "2.4.11"
      Seq(
        "com.typesafe.akka"  %% "akka-actor"       % akkaV,
        "com.typesafe.akka"  %% "akka-slf4j"       % akkaV,
        "io.spray"           %% "spray-can"        % sprayV,
        "io.spray"           %% "spray-routing"    % sprayV,
        "io.spray"           %% "spray-json"       % sprayJsonV,
        "org.specs2"         %% "specs2-core"      % specsV      % "test",
        "io.spray"           %% "spray-testkit"    % sprayV      % "test",
        "com.typesafe.akka"  %% "akka-testkit"     % akkaV       % "test",
        // Non-Scala dependencies
        "com.novocode" % "junit-interface" % "0.11" % "test",
        "ch.qos.logback"     % "logback-classic"  % logbackV
      )
  }
  
  scalacOptions ++= Seq(
    "-unchecked",
    "-deprecation",
    "-Xlint",
    "-Ywarn-dead-code",
    "-language:_",
    "-target:jvm-1.7",
    "-encoding", "UTF-8"
  )
  
  testOptions += Tests.Argument(TestFrameworks.JUnit, "-v")
  </code></pre>
  
  <p>Any help appreciated.</p>