0

I am trying to read excel file to save the records inside the file into database.

Below is my html code :

<div ng-controller = "myCtrl">
<input type="file" file-model="myFile"/>
<button ng-click="uploadFile()">upload me</button>
</div>

I am using this Javascript code to read the file :

$scope.uploadFile = function(){
var file = $scope.myFile;
console.log('file is ' );
console.dir(file);
$http({
method : "POST",
url : "/Example/fileUpload",
data : file
 }).then(function mySuccess(response) {
 console.log(response);
}, function myError(response) {
console.log(response);
});
};

Also, the java code I have written to handle this is :

@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
public @ResponseBody ResponseEntity<String> checkTestMapping(@RequestBody Object file){
System.out.println(file);
return null;
}

It is giving me the following error after I select a file (excel file) and click upload.

error: "Unsupported Media Type" ​​ message: "Content type 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8' not supported" ​​ path: "/Example/fileUpload" ​​ status: 415 ​​ timestamp: "2018-06-14T17:11:27.494+0000"

Can someone help me with this ?

1 Answer 1

0

You need to accept MultipartFile type instead of generic Object type-

@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
public @ResponseBody ResponseEntity<String> checkTestMapping(@NotNull @RequestBody MultipartFile file){

}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. It is now hitting my java code but the file object is coming as null.
That's your angular-side problem. I used a module this module to upload file from angularjs - github.com/danialfarid/ng-file-upload. Please mark the answer as accepted if it solved your controller problem. Feel free to ask me further questions if you need help.
Can you help me with the Angular Side issue also. Thanks.
Umm not sure though but can you try this and give me a feedback - data: { 'file': file }
Still facing the same issue.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.