Grails File Upload Problems

后端 未结 4 2104
花落未央
花落未央 2021-02-06 06:40

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:



        
4条回答
  •  温柔的废话
    2021-02-06 06:54

    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'
    

提交回复
热议问题