I am doing project in Spring java. Need to upload the image without using form submit and use only ajax call of jquery (Since i need to process back the image in the same page)
I have input tag with type=file and
I have already loaded commons-fileupload, commons-io jar in spring maven and also added multipartResolver
Now I am using below code for servlet
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> upload(@RequestParam("file") MultipartFile file) {
Map<String, Object> map = Maps.newHashMap();
try {
BufferedImage src = ImageIO.read(new ByteArrayInputStream(file.getBytes()));
File destination = new File("C:/Users/XXX/Desktop/Image_files/Image1.png");
ImageIO.write(src, "png", destination);
} catch(Exception e) {
e.printStackTrace();
}
return map;
}
my js file
$("#uploadbutton").on("click", function() {
var oMyForm = new FormData();
oMyForm.append("file", $("#imageid").prop("files")[0]);
$.ajax({
url: "/photo/upload/upload",
data: oMyForm,
processData: false,
contentType: false,
type: 'POST',
success: function(response) {
alert("success");
},
error: function() {
alert("unable to create the record");
}
});
});
The problem is I can able to select image in input type=file tag, but when i click upload button, onclick event triggers and does not make ajax call, showing the error messages "405 method not allowed", Kindly do me favour ???? Eagerly waiting the positive reply .... Any alternative way also warmly welcome