播放框架CRUD文件上传 [英] Play framework CRUD file upload
问题描述
#{form action:@save(object._key()),enctype :'multipart / form-data'}
#{crud.form}
#{crud.custom'file'}
< label for =uploadFile>
文件
< / label>
< input type =fileid =uploadFilename =uploadFile/>
#{/ crud.custom}
#{/ crud.form}
< p class =crudButtons>
< input type =submitname =_ savevalue =& {'crud.save',type.modelName}/>
< input type =submitname =_ saveAndContinuevalue =& {'crud.saveAndContinue',type.modelName}/>
< / p>
#{/ form}
但是我不知道如何编写控制器方法处理上传。
我不想将文件存储在数据库中作为一个blob,我不想在文件系统上。
模型
包模型;
import play.db.jpa.Blob;
import play.db.jpa.Model;
import javax.persistence.Entity;
@Entity
public class MyApp extends Model {
public String name;
公共Blob文件;
$ b
模板
#{form action:@create(),enctype:'multipart / form-data'}
#{crud.form /}
< label for =uploadFile>文件< / label>
< input type =fileid =uploadFilename =myapp.file/>
< p class =crudButtons>
< input type =submitname =_ save
value =& {'crud.save',type.modelName}/>
< input type =submitname =_ saveAndContinue
value =& {'crud.saveAndContinue',type.modelName}/>
< / p>
$ {/ form}
控制器
程序包控制器
import play。*;
import play.mvc。*;
import java.util。*;
导入模型。*;
/ *从CRUD模块扩展
*控制器的自定义控制器。
* /
public class MyController extends CRUD {
// ...
//将保存对象
public static void创建(MyApp对象){
/ *获取当前类型的控制器并在非空* /
上进行测试ObjectType type = ObjectType.get(getControllerClass());
notFoundIfNull(type);
/ *我们执行生成的crud模块表单域验证* /
validation.valid(object);
if(validation.hasErrors()){
renderArgs.put(error,Messages.get(crud.hasErrors));
尝试{
render(request.controller.replace(。,/)+/blank.html,type,object);
catch(TemplateNotFoundException e){
render(CRUD / blank.html,type,object);
$ b $ *将我们的对象保存到db * /
object._save();
$ b $ *显示消息* /
flash.success(Messages.get(crud.created,type.modelName));
if(params.get(_ save)!= null){
redirect(request.controller +.list);
if(params.get(_ saveAndAddAnother)!= null){
redirect(request.controller +.blank);
$ b $ p如上所述,通过自己的领域克服形式和压倒crud方法创造。同样可以做更新记录。
您可以在application.conf中更改data / attachments目录:
$ b $ h2 application.conf
#...
#存放Blob内容的路径
attachments.path = data / attachments
#...
code>
有关更多详细信息,请参阅 http://www.lunatech-research.com/playframework-file-upload-blob
anyone know a way to add a file upload to Play's CRUD form? So far I have a view part like this:
#{form action:@save(object._key()), enctype:'multipart/form-data'}
#{crud.form}
#{crud.custom 'file'}
<label for="uploadFile">
File
</label>
<input type="file" id="uploadFile" name="uploadFile" />
#{/crud.custom}
#{/crud.form}
<p class="crudButtons">
<input type="submit" name="_save" value="&{'crud.save', type.modelName}" />
<input type="submit" name="_saveAndContinue" value="&{'crud.saveAndContinue', type.modelName}" />
</p>
#{/form}
But I don't know how to write the controller method to handle the upload.
And I don't want to store the file in the db as a blob, I wan't it on filesystem.
解决方案 This code will save your file into a "data/attachments" directory of your project:
Model
package models;
import play.db.jpa.Blob;
import play.db.jpa.Model;
import javax.persistence.Entity;
@Entity
public class MyApp extends Model {
public String name;
public Blob file;
}
Template
#{form action:@create(), enctype:'multipart/form-data'}
#{crud.form /}
<label for="uploadFile">File</label>
<input type="file" id="uploadFile" name="myapp.file" />
<p class="crudButtons">
<input type="submit" name="_save"
value="&{'crud.save', type.modelName}" />
<input type="submit" name="_saveAndContinue"
value="&{'crud.saveAndContinue', type.modelName}" />
</p>
#{/form}
Controller
package controllers
import play.*;
import play.mvc.*;
import java.util.*;
import models.*;
/* Custom controller that extends
* controller from CRUD module.
*/
public class MyController extends CRUD {
// ...
// Will save your object
public static void create(MyApp object) {
/* Get the current type of controller and test it on non-empty */
ObjectType type = ObjectType.get(getControllerClass());
notFoundIfNull(type);
/* We perform validation of the generated crud module form fields */
validation.valid(object);
if (validation.hasErrors()) {
renderArgs.put("error", Messages.get("crud.hasErrors"));
try {
render(request.controller.replace(".", "/") + "/blank.html", type, object);
} catch (TemplateNotFoundException e) {
render("CRUD/blank.html", type, object);
}
}
/* Save our object into db */
object._save();
/* Show messages */
flash.success(Messages.get("crud.created", type.modelName));
if (params.get("_save") != null) {
redirect(request.controller + ".list");
}
if (params.get("_saveAndAddAnother") != null) {
redirect(request.controller + ".blank");
}
}
As stated above, you simply complements crud form by your own field and overrides crud method "create". The same can be done to update the record.
You can change "data/attachments" directory in your application.conf:
application.conf
# ...
# Store path for Blob content
attachments.path=data/attachments
# ...
For more details see http://www.lunatech-research.com/playframework-file-upload-blob
这篇关于播放框架CRUD文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!