The following code snippet shows how to save the downloaded file into MongoDB directly:
object MyController extends Controller { ... def saveImage = Action.async(fsBodyParser) { implicit request => val result = for { file <- request.body.files.head.ref update <- { fsService.update( file.id, Json.obj("metadata" -> Json.obj("category" -> "image")) ) } } yield update result.map { _ => Created(success).withHeaders(LOCATION -> s"${localHost.baseUrl}${request.uri}") } } private def fsBodyParser()( implicit fsService: FsServiceComponent#FsService ): BodyParser[MultipartFormData[Future[MetaFile]]] = { import BodyParsers.parse._ multipartFormData(Multipart.handleFilePart { case Multipart.FileInfo(partName, filename, contentType) => fsService.iteratee(filename, contentType) }) } }
The above code compiles and works correctly until Play 2.3.x ... while, if I try to compile it using Play 2.4.x, I always get the following error messages:
[error] /home/j3d/Projects/test/app/controllers/MyController.scala:71: not found: value handleFilePart [error] multipartFormData(handleFilePart { [error] ^ [error] /home/j3d/Projects/test/app/controllers/MyController:72: not found: value FileInfo [error] case FileInfo(partName, filename, contentType) => [error] ^ [error] (compile:compile) Compilation failed [error] Total time: 2 s, completed Jan 3, 2015 2:11:47 PM
Look at the latest version of Multipart.scala ... Multipart.handleFilePart now private , and there seems to be no other option than handleFilePartAsTemporaryFile . What for? Is there a workaround?
source share