Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails File Upload Problems

I'm trying to emulate the file upload code from the grails website, and I'm running into some problems. I'm using the same code as found here. Here is my code:

    <g:form action="upload" method="post" enctype="multipart/form-data">
        <input type="file" name="myFile" />
        <input type="submit" value="Upload" />
    </g:form>

and

def upload = {
    def f = request.getFile('myFile')
    if(!f.empty) {
      flash.message = 'success'
    }    
    else {
       flash.message = 'file cannot be empty'
    }
}

I'm receiving the following error at runtime:

Message: No signature of method: org.mortbay.jetty.Request.getFile() is applicable for argument types: (java.lang.String) values: {"myFile"}
Caused by: groovy.lang.MissingMethodException: No signature of method: org.mortbay.jetty.Request.getFile() is applicable for argument types: (java.lang.String) values: {"myFile"}

It appears to be related to some Spring configuration. Spring does not appear to be injecting MultipartHttpServletRequest, so my request doesn't have the appropriate method. I just created this applications using grails create-app. I have not modified the resources.groovy file. I'm using grails 1.0.3.

Any help is much appreciated. The grails website makes this look so easy.

like image 247
anschoewe Avatar asked Oct 15 '08 20:10

anschoewe


4 Answers

Problem solved!

I was using the example code for uploading files to Grails differently than the original author probably intended. The problem is that when the upload method of the controller was called, it was sometimes for the original render of the Upload page. The request in that method was was not of type MultipartHttpServletRequest. When I did a POST with my file to upload, then Spring did the correct thing and changed my requestion to MultipartHttpServletRequest. So, I needed to do a simple check in my update controller method before using my request like a MultipartHttpServletRequest.

if(request instanceof MultipartHttpServletRequest)
{
  MultipartHttpServletRequest mpr = (MultipartHttpServletRequest)request;  
  CommonsMultipartFile f = (CommonsMultipartFile) mpr.getFile("myFile");
  if(!f.empty)
    flash.message = 'success'
  else
   flash.message = 'file cannot be empty'
}   
else
  flash.message = 'request is not of type MultipartHttpServletRequest'
like image 83
anschoewe Avatar answered Nov 06 '22 20:11

anschoewe


Now with the Grails 2.x use:

<g:uploadForm name="upload" action="upload" method="POST">
     <input type="file" name="file" />
</g:uploadForm>

def upload = {
    def file = request.getFile('file')
    assert file instanceof CommonsMultipartFile

    if(!file.empty){ //do something }
    else { //do something }
}

More clean, more simple.

like image 24
Carleto Avatar answered Nov 06 '22 19:11

Carleto


make sure you update the html (your gsp with the form to upload from) to have the enctype as they show:

<g:form action="upload" method="post" enctype="multipart/form-data">

Hope that is helpful, seems too obvious but it's my first thought after seeing your error message.

like image 41
codeLes Avatar answered Nov 06 '22 18:11

codeLes


Someone here seems to be having the same troubles you had. He says he "fixed" it:

Solved. It was my mistake, I was going in action save before submitting the form, so I suppose there was no file.

Not sure how to take what he said, but maybe it'll help you.

like image 43
billjamesdev Avatar answered Nov 06 '22 19:11

billjamesdev