如何在play2中获取其他输入的上传文件? [英] How to get the upload file with other inputs in play2?
问题描述
< form action =@ routes.Files.uploadmethod =postenctype =multipart / form-data>
< input type =hiddenname =groupIdvalue =1/>
< input type =hiddenname =tagIdvalue =2/>
< input type =filename =file/>
< input type =submitvalue =upload it/>
< / form>
如何编写动作文件上传
?
我知道如何获取上传的文件:
请求。 body.file(file)map {
filepart => filepart.ref.moveTo(NEWFILE);
}
以及如何获取提交的输入:
表单(元组(groupId - >文本,tagId - >文本))。 ..,
params => ....
)
但是如何将它们结合在一起?
我没有找到文件的适当类型
可用于 Form(tuple(...))
,既不是获取 request.body
中的输入值的方法。 >
这个答案是针对Java的,但是您应该可以很容易地将其适配到Scala。 b $ b
你需要做的是定义一个模型,除了文件外,你的表单中的所有字段。然后像正常一样使用文件上传API来检索文件。
例如,这就是我所做的:
@form(action = (uploadForm(lang))
@inputText(uploadForm(国家))
@inputFile(uploadForm(resourceFile))
< p>
< input type =submit>
< / p>
$ b 模型(models / UploadResource.java) :
pre $ public class UploadResource {
@Required
public String lang;
@必需
公共字符串国家;
$ b $ *注意缺少一个字段* /
}
控制器(controllers / UploadResourceController.java):
public static Result doUpload ){
表格<上传资源> filledForm = uploadForm.bindFromRequest();
if(filledForm.hasErrors()){
return badRequest(views.html.upload.render(filledForm));
} else {
UploadResource resource = filledForm.get();
MultipartFormData body = request()。body()。asMultipartFormData();
FilePart resourceFile = body.getFile(resourceFile);
$ b $ *检查resourceFile为null,然后提取File对象并处理它* /
}
}
我希望这有助于。
In html, a form with multipart data:
<form action="@routes.Files.upload" method="post" enctype="multipart/form-data">
<input type="hidden" name="groupId" value="1" />
<input type="hidden" name="tagId" value="2" />
<input type="file" name="file"/>
<input type="submit" value="upload it"/>
</form>
How to write the action Files upload
?
I know how to get a uploaded file:
request.body.file("file") map {
filepart => filepart.ref.moveTo(newFile);
}
And how to get submitted inputs:
Form(tuple("groupId" -> text, "tagId" -> text)).bindFromRequest.fold(
errors => ...,
params => ....
)
But how to combine them together?
I don't find a suitable type for file
can be used in Form(tuple(...))
, and neither a way to get input value in request.body
.
This answer is for Java, but you should be able to adapt it to Scala fairly easily.
What you need to do is define a Model for all the fields in your form except the file. Then use the file-upload API as normal to retrieve the file.
For example, this is what I did:
The Form (in upload.scala.html):
@form(action = routes.UploadResourceController.doUpload(), 'enctype -> "multipart/form-data") {
@inputText(uploadForm("lang"))
@inputText(uploadForm("country"))
@inputFile(uploadForm("resourceFile"))
<p>
<input type="submit">
</p>
}
The Model (models/UploadResource.java):
public class UploadResource {
@Required
public String lang;
@Required
public String country;
/* notice a field for the file is missing */
}
The Controller (controllers/UploadResourceController.java):
public static Result doUpload() {
Form<UploadResource> filledForm = uploadForm.bindFromRequest();
if (filledForm.hasErrors()) {
return badRequest(views.html.upload.render(filledForm));
} else {
UploadResource resource = filledForm.get();
MultipartFormData body = request().body().asMultipartFormData();
FilePart resourceFile = body.getFile("resourceFile");
/* Check resourceFile for null, then extract the File object and process it */
}
}
I hope this helps.
这篇关于如何在play2中获取其他输入的上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!