spring Grails 文件上传问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/206224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Grails File Upload Problems
提问by anschoewe
I'm trying to emulate the file upload code from the grails website, and I'm running into some problems. I'm using the same code as found here. Here is my code:
我正在尝试从 grails 网站模拟文件上传代码,但遇到了一些问题。我使用的代码与此处找到的相同。这是我的代码:
<g:form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="myFile" />
<input type="submit" value="Upload" />
</g:form>
and
和
def upload = {
def f = request.getFile('myFile')
if(!f.empty) {
flash.message = 'success'
}
else {
flash.message = 'file cannot be empty'
}
}
I'm receiving the following error at runtime:
我在运行时收到以下错误:
Message: No signature of method: org.mortbay.jetty.Request.getFile() is applicable for argument types: (java.lang.String) values: {"myFile"}
Caused by: groovy.lang.MissingMethodException: No signature of method: org.mortbay.jetty.Request.getFile() is applicable for argument types: (java.lang.String) values: {"myFile"}
It appears to be related to some Spring configuration. Spring does not appear to be injecting MultipartHttpServletRequest, so my request doesn't have the appropriate method. I just created this applications using grails create-app. I have not modified the resources.groovy file. I'm using grails 1.0.3.
它似乎与某些 Spring 配置有关。Spring 似乎没有注入MultipartHttpServletRequest,所以我的请求没有合适的方法。我刚刚使用grails create-app. 我没有修改 resources.groovy 文件。我正在使用 grails 1.0.3。
Any help is much appreciated. The grails website makes this look so easy.
任何帮助深表感谢。grails 网站让这看起来很简单。
回答by anschoewe
Problem solved!
问题解决了!
I was using the example code for uploading files to Grails differently than the original author probably intended. The problem is that when the uploadmethod of the controller was called, it was sometimes for the original render of the Upload page. The request in that method was was not of type MultipartHttpServletRequest. When I did a POST with my file to upload, then Spring did the correct thing and changed my requestion to MultipartHttpServletRequest. So, I needed to do a simple check in my updatecontroller method before using my request like a MultipartHttpServletRequest.
我使用示例代码将文件上传到 Grails 的方式可能与原作者的意图不同。问题是在调用控制器的upload方法的时候,有时是为了Upload页面的原始render。该方法中的请求不是 MultipartHttpServletRequest 类型。当我对要上传的文件进行 POST 时,Spring 做了正确的事情并将我的请求更改为 MultipartHttpServletRequest。因此,在使用我的请求(如 MultipartHttpServletRequest)之前,我需要在我的更新控制器方法中做一个简单的检查。
if(request instanceof MultipartHttpServletRequest)
{
MultipartHttpServletRequest mpr = (MultipartHttpServletRequest)request;
CommonsMultipartFile f = (CommonsMultipartFile) mpr.getFile("myFile");
if(!f.empty)
flash.message = 'success'
else
flash.message = 'file cannot be empty'
}
else
flash.message = 'request is not of type MultipartHttpServletRequest'
回答by Carleto
Now with the Grails 2.x use:
现在使用 Grails 2.x 使用:
<g:uploadForm name="upload" action="upload" method="POST">
<input type="file" name="file" />
</g:uploadForm>
def upload = {
def file = request.getFile('file')
assert file instanceof CommonsMultipartFile
if(!file.empty){ //do something }
else { //do something }
}
More clean, more simple.
更干净,更简单。
回答by codeLes
make sure you update the html (your gsp with the form to upload from) to have the enctypeas they show:
确保更新 html(带有要上传的表单的 gsp)以具有显示的enctype:
<g:form action="upload" method="post" enctype="multipart/form-data">
Hope that is helpful, seems too obvious but it's my first thought after seeing your error message.
希望这是有帮助的,似乎太明显了,但这是我看到您的错误消息后的第一个想法。
回答by billjamesdev
Someone hereseems to be having the same troubles you had. He says he "fixed" it:
这里的某个人似乎遇到了与您相同的麻烦。他说他“修复”了它:
Solved. It was my mistake, I was going in action save before submitting the form, so I suppose there was no file.
解决了。这是我的错误,我在提交表单之前进行了保存,所以我想没有文件。
Not sure how to take what he said, but maybe it'll help you.
不知道如何接受他说的话,但也许它会帮助你。

