HTML5拖放多文件上传grails插件

时间:2015-12-30 22:01:33

标签: grails grails-plugin

我使用HTML5 drag and drop multi-file upload plugin,在我的Grails应用程序中上传一些文件,但我想将上传的文件保存到数据库,但我不知道持有该文件的对象是什么我在请求和参数中搜索过的上传文件,这是我在 _form 视图中的标记:

<uploadr:add name="myUploadrName"  controller="photos" action="save"   direction="up" maxVisible="8" unsupported="/my/controller/action" rating="false" voting="false" colorPicker="false" maxSize="204800" />

这是创建视图:

<g:form  url="[resource:photosInstance]" enctype="multipart/form-data"><fieldset class="form">
                <g:render template="form"/>
            </fieldset>
            <fieldset class="buttons">
                <g:submitButton id = "submitBtn" name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
            </fieldset>
        </g:form>

这是保存操作:

def save(Photos photosInstance) {
    if (photosInstance == null) {
        notFound()
        return
    }

    if (photosInstance.hasErrors()) {
        respond photosInstance.errors, view:'create'
        return
    }

    request.getFileNames().each{
        request.getFiles(it).each { file ->
            // loop through all files selected
            println "name: $file.name Originalfilename: $file.originalFilename contentType: $file.contentType"
            photosInstance= new Photos(photo:file.bytes).save(failOnError:true)

        }
    }

    /*request.withFormat {
     form multipartForm {
     flash.message = message(code: 'default.created.message', args: [
     message(code: 'photos.label', default: 'Photos'),
     photosInstance.id
     ])
     redirect photosInstance
     }
     '*' { respond photosInstance, [status: CREATED] }
     }*/
    }

1 个答案:

答案 0 :(得分:0)

当我没有控制器代码时,有点难以帮助,但试试这个:

UploadedFile uploadedFile = UploadedFile.get(params.fileId)
if (!uploadedFile) response.sendError(404, "No uploaded file could be found matching id: ${params.fileId}.")

GridFSFile gridFSFile = uploadedFile.file
if (!gridFSFile) response.sendError(404, "No file attached to UploadedFile")