uploading a file in grails

2020-02-28 05:47发布

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.

标签: grails input
3条回答
▲ chillily
2楼-- · 2020-02-28 06:17

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

查看更多
何必那么认真
3楼-- · 2020-02-28 06:19

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

查看更多
神经病院院长
4楼-- · 2020-02-28 06:40

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)

查看更多
登录 后发表回答