在 grails 中上传文件 [英] uploading a file in grails

查看:34
本文介绍了在 grails 中上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 gsp 中上传 grails 文件:

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]

任何想法为什么会像我一样并且在我的其他控制器中使用 getFile 工作正常.

Any ideas why this is as I have and are using getFile in my other controllers which works fine.

推荐答案

这里是工作文件提交:

表格(gsp)

<form method="post" enctype="multipart/form-data">
<p><input type='file' name="cfile"/></p>
<input type='submit'>
</form>

将提交的文件存储到'D:/submitted_file'的控制器:

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()}]");
        }
    }
}

这对我有用(grails 2.0.4)

this works for me (grails 2.0.4)

这篇关于在 grails 中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆