views:

3942

answers:

4

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 inject 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.

Andrew

A: 

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.

codeLes
I have that but it still doesn't work. I've added my exact code to my original post above.
anschoewe
thanks, see your update... trying to dig in... is it wierd that the Request is Jetty specific and not just an HttpRequest...
codeLes
Admittedly I'm new to Grails. I assumed since I was running my app using Jetty, it was normal for the request to by of type Jetty.
anschoewe
+1  A: 

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.

Bill James
saw that, but had no idea how to communicate it... another guy says he did a clean and cleared his cache and it worked... not sure about that... but it worked for him somehow
codeLes
I'm not sure how I go to the action before submitting. Is ths possible? I've cleaned my applications (grails clean), but I don't know how to clear caches.
anschoewe
+5  A: 

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'
anschoewe
Oh, ya.... you never want to allow POST and GET from the same action closure, just for this kind of thing.
Bill James
A: 

Hi guy,

I have problem with Grails file upload. My code is below. Scenario is that I want to redirect or render to uploadForm.GSP again when user has not filled in file name to upload. Unfortunately, if i use redirect with passing params or render, it will fail. However, if i use only redirect without passing params. It works fine. In my scenarios, i have another text field and i want grails to remeber text fields which already filled in. Then I have to use render in stead of redirect. Now I can not find any way to make it work as I think this is issues of grails. I tried both 1.03 and 1.0.4. Both of them giving me same result. Anyther thing I found from grails 1.0.4, when you generate view eg User.groovy. This will create "userInstance" as your bean name. However, in 1.0.3, it will generate "user" as bean name which seem reasonably than 1.0.4 which add "Instance" extra word after bean name




def upload = {

    def f = request.getFile('myFile')
    if(!f.empty) {
      f.transferTo( new File("C:/test/myfile.txt") )
      response.sendError(200,'Done');
    }
    else {
       /---- These two will fail-----/ 
       //redirect(action: "upload", params:params)
       //render(view: "uploadForm")

       /----This will pass----/
       redirect(action: "upload")
    }

}