源码:https://git.oschina.net/leqisoft/JianBook_scala
ArticalController.scala:
/**
* 上传文件
* @return
*/
def upload()=Action(parse.multipartFormData){ request=>{
request.body.file("audio").map( audio=>{
//文件名
val fileName = audio.filename
val contentType = audio.contentType
val toFile = new File(s"/tmp/$fileName")
toFile.createNewFile()
//上传文件保存到指定位置
audio.ref.moveTo(toFile,true)
Ok(JSONUtil.toSuccessJSON())
}
).getOrElse(
Ok(JSONUtil.toErrorJSON())
)
}
}
routs文件配置:
POST /artical/upload controllers.ArticalController.upload()
Html文件:
<form action="/artical/upload" method="post" enctype="multipart/form-data">
<table >
<tr>
<td>
audio:
</td>
<td>
<input name="audio" type="file">
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"></td>
</tr>
</table>
</form>