У меня вот такая форма:
<form id="photoUpload" action="<c:url value="/profile /uploadPhotoFromFile"/>" method="post" enctype="multipart/form-data">
<div style="margin-top: 100px;" align="center">
<p>
<img src="<c:url value="/profile/showPhoto"/>" height="250" width="190" alt=""/>
</p>
<br>
<br>
<a class="submitButton" style="position: relative; overflow: hidden;">
<spring:message code="label.select" />
<input type="file" class="file_upload" name="photo" style="position: absolute; right: 0px; top: 0px; margin: 0px; padding: 0px; cursor: pointer; opacity: 0;" id="fileName" onchange="">
</a>
<button style="margin-left: 10px" class="submitButton"><spring:message code="label.show" /></button>
<br>
</form>
Вот методы в контроллере:
@RequestMapping(value = "/profile/uploadPhoto", method = RequestMethod.GET)
public String getPhotoForm(Model model, @ModelAttribute("userPhoto") UserPhoto photo) {
User user = UserContext.getCurrentUser();
UserPhoto userPhoto = new UserPhoto();
userPhoto.setPhoto(user.getPhoto());
model.addAttribute("userPhoto", userPhoto);
return UPLOAD_PHOTO_VIEW;
}
@RequestMapping(value = "/profile/uploadPhoto", method = RequestMethod.POST)
public String submitPhoto(@ModelAttribute("userPhoto") @Valid UserPhoto photo, BindingResult result, HttpServletRequest request, Model model) {
User user = UserContext.getCurrentUser();
UserPhoto userPhoto = new UserPhoto();
userPhoto.setPhoto(user.getPhoto());
model.addAttribute("userPhoto", userPhoto);
return UPLOAD_PHOTO_VIEW;
}
@RequestMapping(value = "/profile/showPhoto", method = RequestMethod.GET)
public ModelAndView service(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
String offset = request.getParameter("offset");
response.setDateHeader("Expires", System.currentTimeMillis() + 24 * 60 * 60 * 1000);
try {
response.setContentType("image/jpeg");
OutputStream out = response.getOutputStream();
User user = UserContext.getCurrentUser();
try {
response.setContentLength(user.getPhoto().length);
out.write(user.getPhoto());
} finally {
out.close();
}
return null;
} catch (Exception e) {
throw new ServiceException(e.getMessage());
}
}
Почему-то по нажатию на кнопку "show" катринка не перерисовывается. Метод "showPhoto" не вызывается. И как сделать перерисовку сразу после выбора файла (который выбирается при нажатии кнопки с type="file") автоматически без нажатия кнопки "show"?