我正在尝试使用以下代码在Play2.1中使用ReactiveMongo保存附件:

def upload = Action(parse.multipartFormData) { request =>
    request.body.file("carPicture").map { picture =>
    val filename = picture.filename
    val contentType = picture.contentType

    val gridFS = new GridFS(db, "attachments")
    val fileToSave = DefaultFileToSave(filename, contentType)

    val futureResult: Future[ReadFile[BSONValue]] = gridFS.writeFromInputStream(fileToSave, new FileInputStream(new File(filename)))

    Ok(Json.obj("e" -> 0))
  }.getOrElse {
    Redirect(routes.Application.index).flashing(
      "error" -> "Missing file"
    )
  }
}

我得到以下错误:
找不到参数readfilereader的隐式值:reactivemongo.bson.bsondocumentreader[reactivemongo.api.gridfs.readfile[reactivemongo.bson.bson value]]
[错误]val futuresult:future[readfile[bsonvalue]]=gridfs.writefromputstream(filetosave,new fileinputstream(new file(filename)))
我错过了什么?
谢谢,
遗传算法

最佳答案

很可能在作用域中没有DefaultReadFileReader隐式对象,可以通过添加导入来解决此问题:

import reactivemongo.api.gridfs.Implicits._

以下编译对我来说很好(使用play 2.1 reactiveMongo模块0.9版):
package controllers

import java.io.{ File, FileInputStream }
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

import play.api._
import play.api.mvc._
import play.api.libs.json._

import reactivemongo.api._
import reactivemongo.bson._
import reactivemongo.api.gridfs._
import reactivemongo.api.gridfs.Implicits._

import play.modules.reactivemongo.MongoController


object Application extends Controller with MongoController {

  def index = Action {
    Ok(views.html.index("Hello, world..."))
  }

  def upload = Action(parse.multipartFormData) { request =>
    request.body.file("carPicture").map { picture =>
      val filename = picture.filename
      val contentType = picture.contentType

      val gridFS = new GridFS(db, "attachments")
      val fileToSave = DefaultFileToSave(filename, contentType)

      val futureResult: Future[ReadFile[BSONValue]] = gridFS.writeFromInputStream(fileToSave, new FileInputStream(new File(filename)))

      Ok(Json.obj("e" -> 0))
    }.getOrElse {
      Redirect(routes.Application.index).flashing(
        "error" -> "Missing file"
      )
    }
  }
}

10-05 20:41