我一直试图在我的Scala Play服务器上上传文件。我已经按照
Play framework documentation page dealing with file uploads。按照此处提供的说明,我首先在TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.media',
],
},
},
]
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
文件夹中创建了一个名为fileuploadform.scala.html
的HTML页面。该文件包含以下代码
views
然后,我在控制器中创建了两个动作。一个将满足GET请求以加载@helper.form(action = routes.ScalaFileUploadController.upload, 'enctype -> "multipart/form-data") {
<input type="file" name="picture">
<p>
<input type="submit">
</p>
}
html的请求,另一个将响应POST请求,方法是单击表单上的Upload按钮。
控制器(fileuploadform
)中的两个动作是:
ScalaFileUploadController.scala
和
def uploadForm = Action {
Ok(views.html.fileuploadform())
}
最后,在路线中,我将路线定义为
def upload = Action(parse.multipartFormData) { request =>
request.body.file("picture").map { picture =>
val filename = Paths.get(picture.filename).getFileName
picture.ref.moveTo(Paths.get(s"/path/to/location/$filename"), replace = true)
Ok("File uploaded")
}.getOrElse {
Redirect(routes.ScalaFileUploadController.index).flashing(
"error" -> "Missing file")
}
}
该应用程序在端口xxxx上运行
当我点击网址GET /uploadForm controllers.ScalaFileUploadController.uploadForm
POST /upload controllers.ScalaFileUploadController.upload()
时,我收到了编译错误
ip.ip.ip.ip:xxxx/uploadForm
not found: value Paths
行被突出显示。
我是否缺少要添加的库或语法修改?
答案 0 :(得分:1)
您需要导入以下内容:
import java.nio.file.Paths