Grails request.getFile总是返回null

时间:2015-09-30 14:41:46

标签: spring-mvc grails groovy grails-controller grails-3.0

我一直在尝试提交包含多个输入(文本和一个图像)的表单,但我无法从请求中检索图像(Grails 3.0.4)

我的create.gsp看起来像这样:

<g:uploadForm controller="advertisement" action="save">

   <input name="name" type="text" id="advertisement_name" />

   <input name="link" type="text" id="advertisement_url" />

   <input id="add_banner_image" name="myimage" type="file" accept="image/png,image/jpeg" data-max-size="512000"/>

   <g:submitButton name="save" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent" value="Salvar anúncio">
   </g:submitButton>

</g:uploadForm>

我的浏览器显示正确的请求有效负载:

------WebKitFormBoundarysaAgHUi2q9vTfkkY
Content-Disposition: form-data; name="name"

Filipe
------WebKitFormBoundarysaAgHUi2q9vTfkkY
Content-Disposition: form-data; name="link"

https://www.filipescosta.com
------WebKitFormBoundarysaAgHUi2q9vTfkkY
Content-Disposition: form-data; name="imagemlouca"; filename="exemplo.jpeg"
Content-Type: image/jpeg


------WebKitFormBoundarysaAgHUi2q9vTfkkY
Content-Disposition: form-data; name="save"

Salvar anúncio

我的resources.groovy看起来像这样:

beans = {
    multipartResolver (org.springframework.web.multipart.commons.CommonsMultipartResolver) {
        maxUploadSize=2500000
    }
}

我的application.yml看起来像这样:

grails:
mime:
    disable:
        accept:
            header:
                userAgents:
                    - Gecko
                    - WebKit
                    - Presto
                    - Trident
    types:
        all: '*/*'
        atom: application/atom+xml
        css: text/css
        csv: text/csv
        form: application/x-www-form-urlencoded
        html: 
          - text/html
          - application/xhtml+xml
        js: text/javascript
        json:
          - application/json
          - text/json
        multipartForm: multipart/form-data
        pdf: application/pdf
        rss: application/rss+xml
        text: text/plain
        hal: 
          - application/hal+json
          - application/hal+xml
        xml:
          - text/xml
          - application/xml
urlmapping:
    cache:
        maxsize: 1000
controllers:
    defaultScope: singleton
converters:
    encoding: UTF-8
views:
    default:
        codec: html
    gsp:
        encoding: UTF-8
        htmlcodec: xml
        codecs:
            expression: html
            scriptlets: html
            taglib: none
            staticparts: none

我的控制器看起来像这样:

def save() {
    println request.getFile('myimage')
    println "Request class: ${request.class}"
    [...]

    request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.created.message', args: [message(code: 'advertisement.label', default: 'Advertisement'), advertisement.id])
            redirect(uri:'/')
        }
        '*' { respond advertisement, [status: CREATED, formats:['html']] }
    }
}

但是在控制台中,println消息是:

null
Request class: class org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest

任何人都可以看看并帮助我一些技巧吗?我一直在寻找例子,我找不到任何有用的东西。

2 个答案:

答案 0 :(得分:1)

你定义:attribute name =“myimage” 并尝试使用大写名称 - &gt; request.getFile( 'MYIMAGE')

答案 1 :(得分:0)

好吧,我弄明白发生了什么。

我试图用String类型创建一个新对象,但我发送了一个输入文件类型作为参数。 然后,在尝试将输入类型String设置为文件类型后,代码中断了,我无法打印request.getFile的值。