2

I want to upload files using angular js and spring boot.

Here's my java controller

//upload Files

    @RequestMapping(value="/upload",headers=("content-type=multipart/*"), method=RequestMethod.POST)
    public @ResponseBody String handleFileUpload(@RequestParam("name") String name,@RequestParam("file") MultipartFile file){
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                BufferedOutputStream stream =
                        new BufferedOutputStream(new FileOutputStream(new File(name)));
                stream.write(bytes);
                stream.close();
                return "You successfully uploaded " + name + "!";
            } catch (Exception e) {
                return "You failed to upload " + name + " => " + e.getMessage();
            }
        } else {
            return "You failed to upload " + name + " because the file was empty.";
        }
    }

Here's my form

<section id="contact-info">


    <section id="contact-page">
        <div class="container">
            <div class="center">        

                <p class="lead">Import reports</p>
            </div> 
            <div class="row contact-wrap"> 
                <div class="status alert alert-success" style="display: none"></div>
                <form id="main-contact-form" class="contact-form" name="contact-form" method="POST" enctype="multipart/form-data" >
                    <div class="col-sm-5 col-sm-offset-1">
                        <div class="form-group">
                            <label>name *</label>
                            <input type="text" name="name" class="form-control" required="required" ng-model="rap.name">
                        </div>

            <div class="form-group">
                            <label>file</label>
                            <input type="file" name="file" class="form-control" ng-model="rap.file">
                        </div>                        

                        <div class="form-group">
                            <button type="submit"  class="btn btn-primary btn-lg"  ng-click="upload()">Import File</button>
                        </div>

                    </form> 
            </div><!--/.row-->
        </div><!--/.container-->
    </section><!--/#contact-page-->

Here's my js controller

//Upload files
        $scope.upload=function(rap){
             $http.post('http://localhost:8080/upload?name='+$scope.rap.name+"file="+$scope.rap.file ,{
                     headers: { 'Content-Type': undefined },
                        transformRequest: angular.identity })

             .success(function(){

                 console.log('Post Succeded !');
             })
             .error(function(){
                 console.log('Post Failed .');
             });
        }

When i fill the form and i click on ImportFile , i have the error mentioned below .Any ideas?

Chawqi Hajar
  • 141
  • 4
  • 16

1 Answers1

0
$http.post('http://localhost:8080/uploadname='+$scope.rap.name+"file="+$scope.rap.file ,{
                 headers: { 'Content-Type': undefined },
                    transformRequest: angular.identity })

You signature for this method is a little bit wrong - second object is data, please see https://docs.angularjs.org/api/ng/service/$http#post

"file="+$scope.rap.file

Are you trying to post file contents via url as multipart object? Usually files are posted in a http body.

Also, ngModel doesn't support binding to value of input[file], not all browsers support FileAPI in javascript - please see for example this https://github.com/angular/angular.js/issues/1375

So, if you need to support "legacy" browsers, please use third party ng's polyfill libraries written for this purpose(like @Uzi Kilon suggested).

if you are fine with modern browsers, you can add custom input[file] onchange handler to bind a file to model and post correctly to server endpoint.(see AngularJS: how to implement a simple file upload with multipart form?)

Community
  • 1
  • 1