在grails中上载文件

在grails中上载文件,grails,input,Grails,Input,我正在尝试在gsp中的grails中上载一个文件,我有: <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/> <inpu

我正在尝试在gsp中的grails中上载一个文件,我有:

<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>
请求获取文件失败,出现以下错误:

No signature of method: org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper.getFile() is applicable for argument types: (java.lang.String) values: [myFile]

你知道为什么我在我的其他控制器中使用getFile,并且运行良好。

你需要
g:form
标签上的
enctype=“multipart/form data”
使浏览器使用多部分请求。

这里是工作文件提交:

表格(普惠制)


这对我很有用(Grails2.0.4)

为了上传文件,必须在表单上设置enctype。为此,您可以使用与标准表单标记相同的
,只是它会自动将enctype属性设置为“多部分/表单数据”。

我更喜欢使用图像/文件上传插件将文件附加到域模型、上传到CDN、验证内容或生成缩略图

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
   }
}
普惠制

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


在编辑过程中,您似乎将enctype放在了
输入标签上,但它需要放在表单标签上。
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()}]");
        }
    }
}
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
   }
}
<g:uploadForm name="myUpload" controller="upload" action="updateStatus">
    <input type="file" name="myFile" />
</g:uploadForm>
class PhotoController {
   def upload() {
      def photo = new Photo(params)
      if(!photo.save()) {
         println "Error Saving! ${photo.errors.allErrors}"
      }
      redirect view: "index"
   }
}