Play框架CRUD文件上传 [英] Play framework CRUD file upload

查看:19
本文介绍了Play框架CRUD文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道将文件上传添加到 Play 的 CRUD 表单的方法吗?到目前为止,我有一个这样的视图部分:

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}

但是我不知道如何编写控制器方法来处理上传.而且我不想将文件作为 blob 存储在数据库中,我不想将其存储在文件系统中.

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.

推荐答案

此代码会将您的文件保存到项目的data/attachments"目录中:

This code will save your file into a "data/attachments" directory of your project:

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;

}

模板

#{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}

控制器

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");
    }

}

如上所述,您只需通过自己的字段补充 crud 表单并覆盖 crud 方法create".可以执行相同的操作来更新记录.您可以在 application.conf 中更改数据/附件"目录:

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:

# ...
# Store path for Blob content
attachments.path=data/attachments
# ...

欲了解更多详情,请参见 http://www.lunatech-research.com/playframework-file-upload-blob

For more details see http://www.lunatech-research.com/playframework-file-upload-blob

这篇关于Play框架CRUD文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆