Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uploading a file in grails

Tags:

input

grails

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.

like image 485
Sagarmichael Avatar asked Sep 15 '12 16:09

Sagarmichael


2 Answers

here is working file submit:

the form (gsp)

<form method="post" enctype="multipart/form-data">
<p><input type='file' name="cfile"/></p>
<input type='submit'>
</form>

the controller that will store submitted file into 'D:/submitted_file':

def index() {
    if(params.cfile){
        if(params.cfile instanceof org.springframework.web.multipart.commons.CommonsMultipartFile){
            new FileOutputStream('d:/submitted_file').leftShift( params.cfile.getInputStream() );
            //params.cfile.transferTo(new File('D:/submitted_file'));
        }else{
            log.error("wrong attachment type [${cfile.getClass()}]");
        }
    }
}

this works for me (grails 2.0.4)

like image 138
daggett Avatar answered Oct 22 '22 10:10

daggett


You need enctype="multipart/form-data" on the g:form tag to make the browser use a multipart request.

like image 36
Ian Roberts Avatar answered Oct 22 '22 12:10

Ian Roberts