Using Multipart without Form in Spring MVC

后端 未结 2 1650
旧巷少年郎
旧巷少年郎 2020-12-06 08:18

I had gone through many articles in stackoverflow on this specific topic, after a detailed analysis I have finally dared to post another question on the same topic.

相关标签:
2条回答
  • 2020-12-06 08:24

    Problem in my approach :

    I created a bean for MultiPartResolver. My understanding after resolving the issue is like you define this bean only when you want a specific type of file or something very specific to the application. Although I seek more insight into this and would love to hear from techies of stackoverflow.

    Solution for current problem:

    I would give my source code,

    HTML :

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

    AngularJS :

         var myApp = angular.module('myApp', []);
    
            myApp.directive('fileModel', ['$parse', function ($parse) {
                return {
                    restrict: 'A',
                    link: function(scope, element, attrs) {
                        var model = $parse(attrs.fileModel);
                        var modelSetter = model.assign;
    
                        element.bind('change', function(){
                            scope.$apply(function(){
                                modelSetter(scope, element[0].files[0]);
                            });
                        });
                    }
                };
            }]);
            myApp.controller('myCtrl', ['$scope', '$http', function($scope, $http){
    
                $scope.uploadFile = function(){
                    var file = $scope.myFile;
                    var fd = new FormData();
                    fd.append('file', file);
        //We can send anything in name parameter, 
    //it is hard coded to abc as it is irrelavant in this case.
                    var uploadUrl = "/upload?name=abc";
                    $http.post(uploadUrl, fd, {
                        transformRequest: angular.identity,
                        headers: {'Content-Type': undefined}
                    })
                    .success(function(){
                    })
                    .error(function(){
                    });
                }
    
            }]);
    

    Spring :

    @RequestMapping(value="/upload", method=RequestMethod.POST)
        public 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.";
            }
        }
    

    And @arahant Even though we don't see any document base64 content in the request payload while sending request, angular does send MultiPartFile, here is the screenshot

    Thanks to all the references. If not for these people I wouldn't have solved this problem at all.

    References :

    http://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs

    0 讨论(0)
  • 2020-12-06 08:31

    Using MultipartHttpServletRequest would be a simple option here, which should work without any other change.

    public String handleFileUpload(MultipartHttpServletRequest request) {
        Map<String, MultipartFile> uploadedFiles = request.getFileMap();
        //...
    }
    
    0 讨论(0)
提交回复
热议问题