我试图在我的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/