Pra*_*eek 6 scala playframework-2.0
我是Play和scala的新手.我的要求是提供一个浏览按钮,我们可以选择多个文件并上传这些文件.这是我写的代码:
在scala.html文件中:
<input type="file" name="files" multiple="multiple" id="files" size="30">
Run Code Online (Sandbox Code Playgroud)
在控制器中:
def upload = Action(parse.multipartFormData) { request =>
request.body.file("files").map { picture =>
import java.io.File
val filename = picture.filename
val contentType = picture.contentType
picture.ref.moveTo(new File("/tmp/picture"))
Ok("File uploaded")
}.getOrElse {
Redirect(routes.Application.index).flashing(
"error" -> "Missing file"
)
}
}
Run Code Online (Sandbox Code Playgroud)
但我无法上传多个文件.知道这里有什么问题吗?
首先你不需要
="multiple"
Run Code Online (Sandbox Code Playgroud)
这等效地工作
<input type="file" name="files" multiple id="files" size="30">
Run Code Online (Sandbox Code Playgroud)
enctype="multipart/form-data"
Run Code Online (Sandbox Code Playgroud)
例如,如果使用助手
@helper.form(action = routes.MyController.submit(), 'enctype -> "multipart/form-data", 'id -> "myform")
Run Code Online (Sandbox Code Playgroud)
或者如果你不是
<form action=... enctype="multipart/form-data" id="myform">
Run Code Online (Sandbox Code Playgroud)
在你的控制器中你想尝试这样的事情(对于Java,我确信它在Scala中类似)
//Get all files bound to the form when submitted
List<FilePart> plate_files = request().body().asMultipartFormData().getFiles();
//Get files from a specific name or id
FilePart myfile = request().body().asMultipartFormData().getFile("files");
Run Code Online (Sandbox Code Playgroud)
然后您可以使用这些迭代 FilePart 对象
希望它在 scala 中类似
干杯
| 归档时间: |
|
| 查看次数: |
3330 次 |
| 最近记录: |