【问题标题】:Play framework CRUD file upload播放框架CRUD文件上传
【发布时间】:2011-12-07 16:12:18
【问题描述】:

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

   #{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存储在数据库中,我不想在文件系统上存储它。

【问题讨论】:

    标签: file-upload playframework crud


    【解决方案1】:

    此代码会将您的文件保存到项目的“数据/附件”目录中:

    型号

    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 中更改“data/attachments”目录:

    application.conf

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

    更多详情见http://www.lunatech-research.com/playframework-file-upload-blob

    【讨论】:

    • "令人困惑的是,play.db.jpa.Blob 数据存储在数据库外部的文件中,并且在数据库中不使用带有 BLOB 的 java.sql.Blob 类型。"文章中的那句话真的让我明白了。他是对的,他们确实把它命名为 Blob。
    猜你喜欢
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多