希望创建使用multipart / form-data上传文件的功能。但是我无法掌握如何更改MultipartFormData并将其存储在文件系统中。以下是我到目前为止的情况。
trait Service extends HttpService {
private final lazy val fileWorker = actorRefFactory.actorOf(Props[FileServicesActor])
implicit def executionContext = actorRefFactory.dispatcher
val demoRoute: Route = {
path("file") {
post {
respondWithMediaType(`application/json`) {
entity(as[MultipartFormData]) { formData =>
uploadFile(formData)
}
}
}
}
}
private def uploadFile(data: MultipartFormData)= {
val response = (fileWorker ? UploadFile(data)).mapTo[Any].map { case t: Success => t
case e: Error => Error.outputError(e)
case _ => Failure(_)
}.recover { case e: Exception => Failure(e)
}
complete(response)
}
}
该功能解析为此
def uploadFile(data: MultipartFormData) = {
val file = data.get("file")
//not sure what to do with data here...
}