[英]How to upload a file in play framework?
I am new to play framework 2.0 and wanted to upload a file in my local file system. 我是新玩的框架2.0,并希望在我的本地文件系统中上传文件。 But I have no idea how to start this.can anyone help me here? 但我不知道如何开始这个。任何人都可以帮助我吗?
Our Form 我们的表格
@form(action = routes.Application.upload, 'enctype -> "multipart/form-data") {
<input type="file" name="picture">
<p>
<input type="submit">
</p>
}
Our Upload action 我们的上传行动
@BodyParser.Of(value = BodyParser.Text.class, maxLength = 10 * 1024)
public static Result upload() {
MultipartFormData body = request().body().asMultipartFormData();
FilePart picture = body.getFile("picture");
if (picture != null) {
String fileName = picture.getFilename();
String contentType = picture.getContentType();
File file = picture.getFile();
return ok("File uploaded");
} else {
flash("error", "Missing file");
return redirect(routes.Application.index());
}
}
Just change the maxLength = 10 * 1024
(this is just around 10kb) to your desired length more of this can be found on the documentation 只需将maxLength = 10 * 1024
(这大约10kb)更改为您想要的长度,可以在文档中找到更多
if you are gonna send the files via Ajax. 如果你要通过Ajax发送文件。 use this 用这个
public static Result upload() {
File file = request().body().asRaw().asFile();
return ok("File uploaded");
}
The response from above will be encoded as Mutlipart/form-data
but will just contain the plain content files 上面的响应将编码为Mutlipart/form-data
但只包含普通内容文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.