10

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.

3 Answers 3

8

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)

Sign up to request clarification or add additional context in comments.

Comments

4

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

1 Comment

From your edit it looks like you put the enctype on the input tag, but it needs to be on the form tag instead.
0

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

import com.bertramlabs.plugins.selfie.Attachment

class Book {
   String name
   Attachment photo

   static attachmentOptions = [
       photo: [
           styles: [
              thumb: [width: 50, height: 50, mode: 'fit'],
              medium: [width: 250, height: 250, mode: 'scale']
          ]
       ]
   ]

   static embedded = ['photo'] //required

   static constraints = {
      photo contentType: ['image/jpeg','image/png'], fileSize:1024*1024 // 1mb
   }
}

GSP

<g:uploadForm name="myUpload" controller="upload" action="updateStatus">
    <input type="file" name="myFile" />
</g:uploadForm>

Controller

class PhotoController {
   def upload() {
      def photo = new Photo(params)
      if(!photo.save()) {
         println "Error Saving! ${photo.errors.allErrors}"
      }
      redirect view: "index"
   }
}

Sources

1. uploadFrom

2. selfie plugin

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.