update user image in directory in grails
I have each user's image in my project directory like user1.jpeg,
user2.jpeg and so on. But when I try to change a user image it is
throughing a error. I am not understanding what to do. Here is the error
as follows >>>
Cannot cast object
'org.apache.catalina.core.ApplicationHttpRequest@21740230' with class
'org.apache.catalina.core.ApplicationHttpRequest' to class
'org.springframework.web.multipart.MultipartHttpServletRequest'
And here is my update action >>>
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'
}
}
Can anyone please help me on this please? I am using grails 2.1.0.
EDIT ::
and here is the view for edit user >>>
<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>
No comments:
Post a Comment