I want to rescale an image and return it to the client, and am running into some trouble with sending the image back to the client.
My serverside controller:
@RequestMapping(value = {"/fw/ajax/submit_profileimage.do"})
public void submitFile(HttpServletRequest request, HttpServletResponse response, @RequestParam("file") MultipartFile f) {
try {
InputStream in = new ByteArrayInputStream(f.getBytes());
BufferedImage originalImage = ImageIO.read(in);
BufferedImage newImage = new BufferedImage(MAX_HEIGHT, MAX_WIDTH, BufferedImage.TYPE_INT_RGB);
paintComponent(newImage.getGraphics(), originalImage, getRatio(originalImage));
ImageIO.write(newImage, "png", response.getOutputStream());
response.setContentType("image/png");
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
Clientside Jquery code:
uploadButton.click(function(){
$('#imagePreview').addClass('loading');
form.ajaxSubmit(function(data){
alert(data); //this is where I'd like to handle the image!
});
});
the data that is returned by the ajaxSubmit is:
"<body style="margin: 0px;"><img style="-webkit-user-select: none" src="http://localhost:8080/fairview/ajax/submit_profileimage.do"></body>"
Clearly not an image file.
But when I check the debugger, I can see that the submit_profileimage.do request has succeded, and allegedly returned an image! I have no idea if I've done something wrong on the clientside, or on the serverside.
So the question is: How can I display the image on the clientside?