简体   繁体   中英

How to upload multiple file through angular js

This is my angular code.File upload is happening for a single file but for adding two other files is not happening.Could you please help me in this issue.Please help me I am badly stuck.

$scope.upload = function(){
            console.log("In upload function");
            console.log(deployTaskId);
            $scope.disableUpload = true;

            var uploadUrl = baseurl+"/"+encodeURIComponent(deployTaskId);
            alert(baseurl);
            console.log(uploadUrl);
            alert(uploadUrl);
            var file = $scope.deploy.myFile;
            var fd = new FormData();
            fd.append('file', file);
            var httpRequest = $http({
            method: 'PUT',
            url: uploadUrl ,
            transformRequest: angular.identity,
             headers: {
            'Content-Type': 'application/json',
            'x-api-key': api_key
            },
            data: fd
            })
            .success(function(data, status){

            console.log("Success in uploading task file");
            $scope.disableUpload = false;
            console.log(data);
            console.log("upload Successful");
            })
            .error(function(data, status){
            console.log("Error in uploading task file");
            $scope.disableUpload = false;
            }); 
        };

This is my directive

task.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]);
                });
            });
        }
    };
}]);

I am personally using angular-file-upload , this directive makes it easy to upload multiple files.

You can find samples on how to upload several files on the README.md.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM