Multipart File Upload using AngularJS and SpringMVC

后端 未结 2 1525

I am new to angularJS and trying to upload a file using angular JS and Spring MVC, but not able to get the required solution and ending up with exceptions in JS Controller.

2条回答
  •  孤街浪徒
    2020-12-04 02:00

    Add these .js file where you have added angular.js files angular-file-upload.js,angular-file-upload-shim.js,ng-file-upload.js,ng-file-upload-shim.js

    You can download from this link Angular File For Upload.

    Then Add ngFileUpload,'angularFileUpload' in angular.module see below line.

    angular.module('formSubmit', [ 'ngFileUpload',
                'angularFileUpload',  'ui.router' ]);
    

    Then Add $upload in your angular controller like this.

    app.controller('FormSubmitController', function($scope, $http, $upload)
    

    Use $upload.upload instead of $http.post in your angular code.

    $upload.upload({
        url : 'doAddQuestion.do',
        file : yourFile,
        data : $scope.questionBean,
        method : 'POST'
    });
    

    Change your spring controller.

    @RequestMapping(value = "/doAddQuestion.do", method = RequestMethod.POST, headers = ("content-type=multipart/*"))
    public @ResponseBody String saveUserDataAndFile(@RequestParam("file") MultipartFile file, QuestionBean questionBean) {
        System.out.println("output: "+questionBean.getQuestion_text());
                // Im Still wotking on it
        return "";
    }
    

提交回复
热议问题