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
def file = request.getFile('file')should bedef file = request.getFile('imageData'). You should also have a single form to submit. More forms are needed only if you want to make AJAX-like file upload - injecteer<g:uploadForm controller="comment" action="update" datatype="file">separately - injecteer