播放2.4.X:如何上传文件而不将其保存到临时文件

时间:2015-01-03 14:24:07

标签: scala file-upload playframework multipartform-data

以下代码段显示了如何将上传的文件直接保存到MongoDB中:

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)
    })
  }
}

上面的代码编译并正常运行到Play 2.3.x ...如果我尝试使用Play 2.4.x编译它,我总是会收到以下错误消息:

[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

现在查看最新版本的Multipart.scala ... Multipart.handleFilePart private,看起来除handleFilePartAsTemporaryFile之外没有其他选项。为什么?有解决方法吗?

0 个答案:

没有答案