AngularJS Formdata file array upload

前端 未结 2 2018
刺人心
刺人心 2020-12-11 21:58

I\'m trying to upload (actually POST) numerous small files in one go along with some key, value pairs:

                $scope.uploadFiles = function(files) {         


        
相关标签:
2条回答
  • 2020-12-11 22:14

    This might be helpful to you.

    Angular:

    $scope.uploadFiles = function(files) {
        if (files.length === 0) {
            return;
        }
    
        var formData = new FormData();
        formData.append('keyName1', 'keyValue1');
        formData.append('keyName2', 'keyValue2');
        formData.append('keyName3', 'keyValue3');
        for (var i = 0; i < files.length; i++) {
            formData.append('file'+i, files[i]);
        }
    
        $http.post( '/myEndpoint', formData, {
            headers: { 'Content-Type': undefined },
            transformRequest: angular.identity
        }).success(function (result) {
            console.log('YAY');
        }).error(function () {
            console.log('NAY');
        });                 
    }
    

    On Spring/Java Side:

    RequestMapping(value = "/myEndpoint", method = RequestMethod.POST)
    public @ResponseBody Object uploadFiles(MultipartHttpServletRequest request, HttpServletResponse response) throws IOException {
        //do stuff here...
        final String keyName1= request.getParameter('keyName1');
        //and so on......
    
        Iterator<String> iterator = request.getFileNames();
        MultipartFile multipartFile = null;
        while (iterator.hasNext()) {
            multipartFile = request.getFile(iterator.next());
            //do something with the file.....
        }
    }
    

    BTW, on you angular side, you can always end the file on one go or with multiple request. It's up to you how you want that implemented.

    0 讨论(0)
  • 2020-12-11 22:28

    I used a library on GitHub to help me accomplish this task with my Java Glassfish Server.

    https://github.com/nervgh/angular-file-upload

    I only needed to upload a single file, and here is the Java Backend to receive that file. This framework does have support to upload multiple files to the server.

    @POST
    @Path("/Endpoint")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response whateverEndPoint(@FormDataParam("fileName") InputStream fileInputStream,
            @FormDataParam("fileName") FormDataContentDisposition contentDispositionHeader,
            @FormDataParam("additionalParameter") String additionalParameter) {
    
        System.out.println(contentDispositionHeader.getFileName());
    
        String output = "File Received on the server: ";
    
        return Response.status(200).entity(output).build();
    
    }
    

    Here is my angular controller that uses the framework:

    angular.module('AppThing').controller('DemoController',function($rootScope,$scope,FileUploader){
    
    //creating the uploader with the correct url
    $scope.uploader = new FileUploader({
        url : 'Endpoint',
        method : 'POST',
        alias: 'fileName',
        autoUpload:true
    });
    
    //runs right after we add a file to the queue
    $scope.uploader.onAfterAddingFile = function(item){
    };
    
    //runs right before we upload an item to the server
    $scope.uploader.onBeforeUploadItem = function(item){
            console.log('This is just before the image upload');
            item.formData.push({'additionalParameter':  $rootScope.additionalParameter});
    };
    
    $scope.uploader.onSuccessItem = function(item, response, status, headers) {
    
    };
    
    
    
    });
    

    Hope this helps

    0 讨论(0)
提交回复
热议问题