grails - 在grails中上传文件

标签 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/>
     <input id="inputField" type="file" name="myFile" enctype="multipart/form-data" />
     <g:submitButton name="Update Status"/>
 </g:form>

在我的 Controller 中,我有:
 def updateStatus(String message) {

        if (params.myFile){
            def f = request.getFile('myFile')

        }

请求获取文件因以下错误而失败:
No signature of method: org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper.getFile() is applicable for argument types: (java.lang.String) values: [myFile]

任何关于为什么这样的想法都可以,并且在其他 Controller 中使用getFile可以正常工作。

最佳答案

这是工作文件提交:

表格(gsp)

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

Controller 将提交的文件存储到“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)

关于grails - 在grails中上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12439135/

相关文章:

java - 获取准确的 int 和 String 输入

html - 输入里面有一个 href/button img

jquery - 我可以引用输入:text as an #id?吗

mongodb - 从Grails更新MongoDB文档

grails - 向用户发送反馈以在Grails中进行长期运行?

security - Grails + Spring Security一次登录

linux - 停止缓冲区溢出 - NASM 输入

grails - 创建简单的插入表单时,Grails字段插件如何工作?

grails - Grails-如果保存时验证为false并且域对象无效,则save()会发生什么?

python - 如何使用枚举键将用户输入添加到字典中?