更新Grails数据库中的图像

时间:2015-07-22 07:52:29

标签: image grails groovy

我有一个可以存储图像的域类。目前我可以创建数据库条目,如果需要上传图像。现在我想在我的DomainController中更改更新方法。该方法应该处理图像的变化,旧图像必须被替换。并且应该有一个删除图像的功能。

My DomainController def update(Class classInstance) - >

    @Transactional
def update(Comment commentInstance) {
    if (commentInstance == null) {
        notFound()
        return
    }

    if (!commentInstance.imageData){
        def file = request.getFile('file')
        if(file.empty) {
            flash.message = "File cannot be empty"
        } else {
            commentInstance.imageData = file.getBytes()
            commentInstance.imageName = file.originalFilename
            commentInstance.comment = params.comment
            commentInstance.feedback = Feedback.get(params.feedback.id)
            commentInstance.user = User.get(params.user.id)

        }
    }
    else
        print "not set yet"

    if (commentInstance.hasErrors()) {
        respond commentInstance.errors, view: 'trash'
        return
    }

    commentInstance.save flush: true

    request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.updated.message', args: [message(code: 'Comment.label', default: 'Comment'), commentInstance.id])
            redirect commentInstance
        }
        '*' { respond commentInstance, [status: OK] }
    }
}

我的edit.gsp段 - >

    <g:if test="${commentInstance?.imageData}">
    <img src="${createLink(controller: "comment", field:"imageData", action:'showImage', id:"${commentInstance.id}")}" width="100"/>
    <br><g:message code="${commentInstance.imageName}" />
</g:if>
<g:else>
    <asset:image src="no_img.png" alt="no_img" width="100"/>
</g:else>
<g:fieldValue bean="${fileInstance}" field="imageName" />

<g:uploadForm controller="comment" action="update" datatype="file">
<input type="file" name="imageData" />
</g:uploadForm>

<g:form url="[resource: commentInstance, action: 'update']" method="PUT">
    <g:hiddenField name="version" value="${commentInstance?.version}"/>
    <fieldset class="form">
        <g:render template="form"/>
    </fieldset>

    <fieldset class="buttons">
        <g:actionSubmit class="save" action="update"
                        value="${message(code: 'default.button.update.label', default: 'Update')}"/>
    </fieldset>

</g:form>

当我尝试通过更新方法上传图像时,我收到此错误:

  

没有方法签名:org.codehaus.groovy.grails.web.filters.HiddenHttpMethodFilter $ HttpMethodRequestWrapper.getFile()适用于参数类型:(java.lang.String)values:[file]   可能的解决方案:getXML(),getPart(java.lang.String),getAt(java.lang.String),getAt(java.lang.String),getLocale(),getJSON()。 Stacktrace如下:   消息:没有方法签名:org.codehaus.groovy.grails.web.filters.HiddenHttpMethodFilter $ HttpMethodRequestWrapper.getFile()适用于参数类型:(java.lang.String)values:[file]   可能的解决方案:getXML(),getPart(java.lang.String),getAt(java.lang.String),getAt(java.lang.String),getLocale(),getJSON()

我如何编辑我的控制器方法来处理这个问题?

Greets Tim

2 个答案:

答案 0 :(得分:0)

查看更正并阅读下面的//comments

 def updateImage(){ // no need for the command object, you need the file itself and the id
    Comment commentInstance = Comment.get params.id
    if( !commentInstance ){
      response.sendError 404 // or some other error signalling
      return
    }
    if(commentInstance.imageData == null){ // I would omit this if, the image should be replaced anyway
        def file = request.getFile('imageData') // must be imageData, according to your GSP
        if(file.empty) {
            flash.message = "File cannot be empty"
        } else {
            commentInstance.imageData = file.bytes
            commentInstance.imageName = file.originalFilename
            commentInstance.save(flush: true, failOnError: true)
        }
    } else {
        print "something else"
    }

    redirect (action:'index')
}

和GSP:

<g:uploadForm controller="comment" action="updateImage" datatype="file">
  <g:hiddenField name="id" value="${commentInstance.id}"/>
  ...
</g:uploadForm>

答案 1 :(得分:0)

在GSP中,动作名称是更新,在控制器中创建的动作是updateImage,似乎没有调用正确的方法。