I am trying to upload a file in grails in my gsp I have:
<g:form id="update" url="[action: 'updateStatus',]">
<g:textArea name="message" value="" cols="3" rows="1"/><br/>
<g:textField id="tagField" name="tag" value=""/><br/>
<input id="inputField" type="file" name="myFile" enctype="multipart/form-data" />
<g:submitButton name="Update Status"/>
</g:form>
In my controller i have:
def updateStatus(String message) {
if (params.myFile){
def f = request.getFile('myFile')
}
The request get file is failing with this error:
No signature of method: org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper.getFile() is applicable for argument types: (java.lang.String) values: [myFile]
Any ideas why this is as I have and are using getFile in my other controllers which works fine.
You need
enctype="multipart/form-data"
on theg:form
tag to make the browser use a multipart request.In order to upload a file you must set the enctype on the form. To do so you can make use of the
<g:uploadForm>
which is identical to the standard form tag except that it sets the enctype attribute to "multipart/form-data" automatically.I prefer to make use of the Grails Selfie Plugin an Image / File Upload Plugin to attach files to your domain models, upload to a CDN, validate content, or produce thumbnails.
Domain
GSP
Controller
Sources
1. uploadFrom
2. selfie plugin
here is working file submit:
the form (gsp)
the controller that will store submitted file into 'D:/submitted_file':
this works for me (grails 2.0.4)