我有一个简单的bean,就像那样:
package models;
import play.data.validation.Constraints;
public class Upload
{
@Constraints.Required
@Constraints.MinLength(4)
@Constraints.MaxLength(40)
public String name;
@Constraints.Required
public String inputFile;
}
和形式,就像那样:
@form(action = routes.Application.submit(), 'enctype -> "multipart/form-data") {
@inputText(
uploadForm("name"),
'_label -> "Name"
)
@inputFile(
uploadForm("inputFile"),
'_label -> "Queries"
)
}
@Required constraint
根本不起作用。
我希望它被选中+在尺寸上添加一些限制。
答案 0 :(得分:1)
使你的表格像:
<input type="file" name="inputFile">
在提交方法中添加以下内容:
// from official documentation
public static Result submit() {
MultipartFormData body = request().body().asMultipartFormData();
FilePart file = body.getFile("inputFile");
if (inputFile != null) {
String fileName = picture.getFilename();
String contentType = picture.getContentType();
File file = picture.getFile();
// method the check size
if(!validateFileSize){
return redirect(routes.Application.index()); // error in file size
}
return ok("File uploaded");
} else {
// here comes the validation
flash("error", "Missing file");
return redirect(routes.Application.index());
}
}
答案 1 :(得分:0)
可能会出现以下情况?
MultipartFormData body = request().body().asMultipartFormData();
if (!body.getFiles().isEmpty()) {
// do your work
}