最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to show progress bar for file upload using angularjs - Stack Overflow

programmeradmin0浏览0评论

i am using this directive

myApp.directive('fileModel', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            var model = $parse(attrs.fileModel);
            var modelSetter = model.assign;
            console.log(modelSetter)
            element.bind('change', function(){
                scope.$apply(function(){
                    modelSetter(scope, element[0].files[0]);
                });
            });
        }
    };
}]);

and my service was

myApp.service('fileUpload', ['$http', function ($http) {
    this.uploadFileToUrl = function(file, uploadUrl){
        var fd = new FormData();
        fd.append('file', file);
        $http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        })
        .success(function(){
        })
        .error(function(){
        });
    }
}]);

in my controller

$scope.uploadFile = function(){
        var file = $scope.myFile;
        console.log('file is ' );
        console.dir(file);
        var uploadUrl = "/fileUpload";
        fileUpload.uploadFileToUrl(file, uploadUrl);
    }; 

my question is how to show progress bar while uploading image to url my JSfiddle URl /

i am using this directive

myApp.directive('fileModel', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            var model = $parse(attrs.fileModel);
            var modelSetter = model.assign;
            console.log(modelSetter)
            element.bind('change', function(){
                scope.$apply(function(){
                    modelSetter(scope, element[0].files[0]);
                });
            });
        }
    };
}]);

and my service was

myApp.service('fileUpload', ['$http', function ($http) {
    this.uploadFileToUrl = function(file, uploadUrl){
        var fd = new FormData();
        fd.append('file', file);
        $http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        })
        .success(function(){
        })
        .error(function(){
        });
    }
}]);

in my controller

$scope.uploadFile = function(){
        var file = $scope.myFile;
        console.log('file is ' );
        console.dir(file);
        var uploadUrl = "/fileUpload";
        fileUpload.uploadFileToUrl(file, uploadUrl);
    }; 

my question is how to show progress bar while uploading image to url my JSfiddle URl https://jsfiddle/JeJenny/ZG9re/

Share Improve this question edited Aug 24, 2017 at 7:11 Sibiraj 4,7668 gold badges37 silver badges59 bronze badges asked Jul 31, 2017 at 6:45 User_3535User_3535 8721 gold badge13 silver badges32 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Try this code, working fine for me

this.uploadFileToUrl = function (file, uploadUrl, progressCB) {
    var fd = new FormData();
    fd.append('file', file);
    $http.post(uploadUrl, fd, {
        transformRequest: angular.identity,
        headers: { 'Content-Type': undefined },
        uploadEventHandlers: {
            progress: progressCB
        }
    })
        .success(function () {
        })
        .error(function () {
        });
}

$scope.uploadFile = function () {
    var file = $scope.myFile;
    console.log('file is ');
    console.dir(file);
    var uploadUrl = "/fileUpload";
    fileUpload.uploadFileToUrl(file, uploadUrl, , function (e) {
        if (e.lengthComputable) {
            var progressBar = (e.loaded / e.total) * 100;
            console.log(progressBar);
            //here you will get progress, you can use this to show progress
        }
    });
}; 

what progressCB presents

Try to use Nprogress

JSFiddle

NProgress.start()

NProgress.done()

I would say. Use this ng-file-upload directive. It has these features inbuilt.

$scope.upload = function (file) {
        Upload.upload({
            url: 'upload/url',
            data: {file: file, 'username': $scope.username}
        }).then(function (resp) {
            console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
        }, function (resp) {
            console.log('Error status: ' + resp.status);
        }, function (evt) {
            var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
            console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
        });
    };

see the demo here

发布评论

评论列表(0)

  1. 暂无评论