grails - HTML5拖放多文件上传grails插件

标签 grails grails-plugin

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

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

这是创建 View :
<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] }
     }*/
    }

最佳答案

当我没有 Controller 代码时,有点难以提供帮助,但是请尝试以下操作:

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

关于grails - HTML5拖放多文件上传grails插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34537905/

相关文章:

Grails 卡在命令行上

grails - 删除 Grails 插件和依赖项

json - 在Grails 2.4.0中将对象呈现为json时排除属性

validation - 向外部系统发送请求之前的其他验证逻辑

grails - Grails quartz 手动触发不起作用

mongodb - 无法使用 mongodb 插件版本 3.0.3 运行 Grails 应用程序

grails - Atmosphere Grails单用户广播

grails - <g :countrySelect> Tag: How to set values as country names instead of country codes

grails - Grails 的 ActiveMQ 插件接管 SSL 连接?

grails 可搜索插件查询