我正在使用playframework 2.1.5。我想发布一个包含数据和文件的表单。我已经定义了一个java类,其中包含数据的String属性和文件的java.io.File。
这是.scala.html方面:
@helper.form(action = routes.Operations.addOperation(), 'enctype -> "multipart/form-data", 'id -> "picture_send_discussion", 'class -> "hide form-inline") {
@helper.inputText(myForm("name"), '_label -> s"${Messages.get("operation.name")} :")
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="input-append">
<div class="uneditable-input span2">
<i class="icon-file fileupload-exists"></i>
<span class="fileupload-preview"></span>
</div>
<span class="btn btn-file">
<span class="fileupload-new">@Messages.get("bm.catalog.selectfile")</span>
<span class="fileupload-exists">@Messages.get("bm.catalog.change")</span>
<input type="file" name="picture"/>
</span>
<a href="#" class="btn fileupload-exists" data-dismiss="fileupload">@Messages.get("all.delete")</a>
</div>
</div>
<button type="submit" class="btn un_btn--primary">@Messages.get("all.save")</button>
选择后,提交的文件属性始终为空。
这是我的控制器代码:
public static Result addOperation() throws IOException {
Form<AddOperationForm> boundForm = addOperationForm.bindFromRequest();
Client user = getClient();
if (boundForm.hasErrors()) {
return badRequest(operation_add_history.render(user, boundForm));
} else {
AddOperationForm co = boundForm.get();
Http.MultipartFormData body = request().body().asMultipartFormData();
Http.MultipartFormData.FilePart picture = body.getFile("picture");
etc ...
返回null的行是这一行:
Http.MultipartFormData body = request().body().asMultipartFormData();