scala - 如何将上传的文件复制到公用文件夹中?
问题描述
请看下面的代码:
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")
}
}
如何获取公共文件夹的路径,以便复制上传的文件?
解决方案
我从这里得到了一个解决方案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")
}
}
推荐阅读
- html - 如何在已对齐页面中心的图像下放置文本?
- video - 如何比较 MPEG-7 视频签名签名
- javascript - React js - 表达:res.send Vs res.render 错误
- c# - .NET Core 3.1 基于角色的授权失败,出现 403 异常
- sql - 如何从一行中选择两个列值中的一个,其中一个是您的查询值,另一个是您在 SQL 中想要的值?
- jquery - 将数组中的数据返回到表中
- kubernetes - Kubernetes:Pod 和 CPU 限制
- python - 如何用熊猫打印所有 csv
- java - 休眠多对一保存。拯救没有孩子的父母。但是应该根据特定属性搜索 Child
- c# - 需要字符串清单以避免 webapi c# 应用程序中的魔术字符串