本文介绍了如何从Servlet3SecurityContextHolderAwareRequestWrapper中获取MultipartRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Grails 2.4.3,我有一个< g:uploadForm> 方法设置为post,但我没有收到 MutlipartRequest 在我的控制器操作中。相反,我得到一个没有 getFile()方法的 Servlet3SecurityContextHolderAwareRequestWrapper 。我已经尝试过投射,我尝试从 request.request 的包装中获取请求,并且我尝试了一些我见过的其他建议给有类似问题的其他人,但仍然没有骰子。我确定我错过了一些简单的东西。我倾向于这样做,但如果任何人都可以指引我正确的方向,我会非常感激。



这是我的表单:

 < g:uploadForm method =POSTaction =uploadSupplemental> 
< div class =modal-header>
< h3 class =modal-titleid =myModalLabel>上传补充数据文件< / h3>
< / div>
< div class =modal-body>
< label for =fileInput>选择要上传的文件:< / label>
< input type =fileid =fileInputname =supplementalData/>
< / div>
< div class =modal-footer>
< button type =buttonclass =btn btn-defaultdata-dismiss =modal>取消< /按钮>
< input type =submitclass =btn btn-primary/>
< / div>
< / g:uploadForm>

以下是我的控制器操作:

<$ p $(){$ b $ def $($)$ def $($) $ b flash.message =找到的文件!!
} else {
flash.message =找不到文件。:-(

重定向操作:'list'
}
code>

以下是我得到的错误:


解决方案

为了启用多部分请求,必须在 Config.groovy 中将以下配置属性设置为 true

  grails.web.disable.multipart = true 

以下是报告相同的异常。



在上传操作之前,请确保用户已通过身份验证(使用 @Secured )。


I'm using Grails 2.4.3 and I have a <g:uploadForm> with method set to post, but I'm not getting a MutlipartRequest in my controller action. Instead I'm getting a Servlet3SecurityContextHolderAwareRequestWrapper which has not getFile() method. I've tried casting, I've tried getting the request out of the wrapper with request.request, and I've tried a bunch of other things I've seen suggested to others with a similar problem, but still no dice. I'm sure I'm missing something simple. I tend to do that, but if anyone can point me in the right direction, I'd be very grateful.

Here's my form:

  <g:uploadForm method="POST" action="uploadSupplemental" >
    <div class="modal-header">
      <h3 class="modal-title" id="myModalLabel">Upload Supplemental Data File</h3>
    </div>
    <div class="modal-body">
      <label for="fileInput">Choose file to upload:</label>
      <input type="file" id="fileInput" name="supplementalData" />
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
      <input type="submit" class="btn btn-primary" />
    </div>
  </g:uploadForm>

And here's my controller action:

  def uploadSupplemental() {
    MultipartRequest multipartRequest =  request as MultipartRequest
    def file = multipartRequest.getFile('supplementalData')
    if (file){
      flash.message = "File found!!"
    } else {
      flash.message = "File NOT found.  :-( "
    }
    redirect action:'list'
  }

And here's the error I get:

解决方案

The following config property has to be set to true in Config.groovy in order to enable multipart requests.

grails.web.disable.multipart=true

Here is a related JIRA issue and a duplicate question in SO reporting the same exception.

Also make sure the user is authenticated (with @Secured) before the upload action.

这篇关于如何从Servlet3SecurityContextHolderAwareRequestWrapper中获取MultipartRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 15:35