I have a file upload feature on my jsp page that specifically accepts json format files. I want to retrieve the uploaded json file and pass it to a Spring controller for further processing.
This is how my javascript code appears:
var file = this.files[0];
var reader = new FileReader();
var oMyForm = new FormData();
oMyForm.append("file", this.files[0]);
console.log('reader='+JSON.stringify(reader)) // The output is empty - "reader={}"
console.log('oMyForm ='+JSON.stringify(oMyForm )) // The output is empty - "oMyForm={}"
var urlpath = "<%=request.getContextPath()%>/fileUpload";
$.ajax({
data : JSON.stringify(reader),
type : 'POST',
dataType: 'json',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
url : urlpath,
success : function(data){
}
});
This is my Java class:
@RequestMapping(value= "/fileUpload", method=RequestMethod.POST,consumes="application/json")
public void fileUpload(){
//I need to retrieve the json object here and perform parsing and manipulations on it
System.out.println("File uploaded!");
}
Despite the file successfully reaching the controller, the browser console displays the error:
"NetworkError: 404 Not Found - http://localhost:8080/proj/fileUpload"
Interestingly, hardcoding a value for JSON.stringify(hardcoded)
works fine.
Json file content:
{'Gyr-x':10.11,'Gyr-y':9.66,'Gyr-z':10.93,'Temparature':30,'Pressure':101,'Humidity':15,'deviceId':1}