4

我正在尝试使用以下代码在 Play 2.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.BSONValue]] [error] val futureResult: Future[ReadFile[BSONValue]] = gridFS.writeFromInputStream(fileToSave,新文件输入流(新文件(文件名)))

我错过了什么?

谢谢,

遗传算法

4

1 回答 1

5

很可能您在范围内没有 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"
      )
    }
  }
}
于 2013-08-05T20:41:14.047 回答