我在项目目录中有每个用户的图像,如user1.jpeg,user2.jpeg等。但是当我尝试更改用户图像时,它正在经历一个错误。我不明白该怎么做。以下是错误>>>
Cannot cast object 'org.apache.catalina.core.ApplicationHttpRequest@21740230' with class 'org.apache.catalina.core.ApplicationHttpRequest' to class 'org.springframework.web.multipart.MultipartHttpServletRequest'
这是我的更新操作>>>
def updateUser = {
String message = ""
MultipartHttpServletRequest mpr = (MultipartHttpServletRequest)request;
CommonsMultipartFile f = (CommonsMultipartFile) mpr.getFile("userPhoto")//getFile("userPhoto");
if(!f.empty) {
def user = User.get(1)
user.avatarType = f.getContentType()
if(user.save()){
def userId = user.id
String username = user.username
println(userId)
String fileName = username + "." + f.getContentType().substring(6)
new File( grailsApplication.config.images.location.toString() ).mkdirs()
f.transferTo( new File( grailsApplication.config.images.location.toString() + File.separatorChar + fileName) )
message = "Here is your updated Information >>> "
render(view: 'userInfo', model: [message: message],)
}else{
message = "Can not Update User !!!"
render(view: 'editUser', model:[message: message])
return;
}
}else {
flash.message = 'file cannot be empty'
}
}
有人可以帮我吗?我使用的是grails 2.1.0。
编辑::
以下是编辑用户>>>
的视图<div class="main">
<g:form controller="user" action="updateUser">
User Name : ${username} <br/>
Photo : <input type="file" name="userPhoto" /> <p></p>
<g:hiddenField name="userId" id="userId" value="${userId}"/>
<g:submitButton name="updateUser" value="Update" />
</g:form>
答案 0 :(得分:0)
// MultipartHttpServletRequest mpr = (MultipartHttpServletRequest)request;
def f = request.getFile("userPhoto")//getFile("userPhoto");
我必须使用<g:uploadForm />
查看http://grails.org/doc/latest/guide/theWebLayer.html#uploadingFiles
答案 1 :(得分:0)
您的代码中的一切都很好,只需在表单标记中添加enctype="multipart/form-data"
,例如
<g:form controller="user" action="updateUser" enctype="multipart/form-data">
...
</g:form>
并且您的代码有效。