Tim P.
Tim P.

Reputation: 5

Update an Image in Grails Database

i've got a domain class in which i can store images. Currently i can create a database entry and if wanted upload an image. Now i want to change the update method in my DomainController. The method should handle a change of the image, the old one has to be replaced. And there should be a function to delete the image.

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] }
    }
}

segment of my 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>

When i try to upload an image via the update method i get this error:

No signature of method: org.codehaus.groovy.grails.web.filters.HiddenHttpMethodFilter$HttpMethodRequestWrapper.getFile() is applicable for argument types: (java.lang.String) values: [file] Possible solutions: getXML(), getPart(java.lang.String), getAt(java.lang.String), getAt(java.lang.String), getLocale(), getJSON(). Stacktrace follows: Message: No signature of method: org.codehaus.groovy.grails.web.filters.HiddenHttpMethodFilter$HttpMethodRequestWrapper.getFile() is applicable for argument types: (java.lang.String) values: [file] Possible solutions: getXML(), getPart(java.lang.String), getAt(java.lang.String), getAt(java.lang.String), getLocale(), getJSON()

How i need to edit my controller method to handle this?

Greets Tim

Upvotes: 0

Views: 530

Answers (2)

injecteer
injecteer

Reputation: 20699

see the corrections and read my //comments below

 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')
}

and GSP:

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

Upvotes: 0

Uday
Uday

Reputation: 629

In the GSP action name is update and the action created in controller is updateImage it seems like right method is not called.

Upvotes: 0

Related Questions