415 (Unsupported Media Type) beim hochladen von Dateien

Möchte ich das hochladen von Dateien über angular js und spring boot.

Hier ist mein 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.";
        }
    }

Hier ist meine 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-->

Hier ist mein 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 .');
             });
        }

Wenn ich das Formular ausfüllen und ich auf ImportFile , ich habe die Fehler, die unten erwähnt werden .Irgendwelche Ideen?

  • Sie können nicht wirklich das hochladen von Dateien über AJAX. werfen Sie einen Blick auf github.com/danialfarid/ng-file-upload
  • Hinzufügen vielleicht consumes = {"multipart/form-data"} zu den @RequestMapping helfen; wenn nicht auch versuchen das weglassen der 'Content-Type': undefined - header, die vom client. Ich bin mir nicht sicher über dieses, nur eine Idee....
  • Nein, es hilft nicht 🙁
  • Uzi Kilon , warum kann ich nicht hochladen von Dateien mit angularjs ?
InformationsquelleAutor Chawqi Hajar | 2015-06-03
Schreibe einen Kommentar