本文介绍了在grails中上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< g:form id =我的gsp上传了一个文件, updateurl =[action:'updateStatus',]>
< g:textArea name =messagevalue =cols =3rows =1/>< br />
< g:textField id =tagFieldname =tagvalue =/>< br />
< input id =inputFieldtype =filename =myFileenctype =multipart / form-data/>
< / g:表格>
在我的控制器中,我有:
<$ p
$ def b = request.getFile('myFile')
如果(params.myFile){
$ b $ def $ update_status
}
请求获取文件失败并出现此错误:
方法没有签名:org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper.getFile()适用于参数类型:(java.lang.String )values:[myFile]
任何想法为什么这是因为我已经在使用getFile控制器工作正常。
解决方案
这里是工作文件提交:
表单(gsp)
< form method =postenctype =multipart / form-data>
< p>< input type ='file'name =cfile/>< / p>
< input type ='submit'>
< / form>
将提交文件存入'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(错误的附件类型[$ {cfile.getClass()}]);
这对我有用(grails 2.0.4)
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]
Any ideas why this is as I have and are using getFile in my other controllers which works fine.
解决方案 here is working file submit:
the form (gsp)
<form method="post" enctype="multipart/form-data">
<p><input type='file' name="cfile"/></p>
<input type='submit'>
</form>
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()}]");
}
}
}
this works for me (grails 2.0.4)
这篇关于在grails中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!