Как скопировать загруженный файл в общую папку? - PullRequest
1 голос
/ 05 октября 2019

Пожалуйста, смотрите код ниже:

def saveFile = Action(parse.multipartFormData) { request =>
  request.body.file("theFile") match {
    case Some(file) =>
      val filename = Paths.get(file.filename).getFileName
      file.ref.copyTo(Paths.get("PATH_TO_PUBLIC_FOLDER" + filename), replace = true)
      Ok("Done")

    case _ =>
      BadRequest("Error with file")
  }
}

Как я могу получить путь к общей папке, чтобы я мог скопировать загруженный файл?

1 Ответ

1 голос
/ 05 октября 2019

Я получил решение отсюда https://stackoverflow.com/a/47778722/2814580

import controllers.AssetsFinder
import play.api.Environment

// After injecting in the controller:
// @Inject()(af: AssetsFinder,env: Environment)

def saveFile = Action(parse.multipartFormData) { request =>
  request.body.file("theFile") match {
    case Some(file) =>
      val baseDir = env.rootPath + af.assetsBasePath + "/"
      val filename = Paths.get(file.filename).getFileName
      file.ref.copyTo(Paths.get(baseDir + filename), replace = true)
      Ok("Done")

    case _ =>
      BadRequest("Error with file")
  }
}
...