3

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.net/JeJenny/ZG9re/

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

5 Comments

getting type error a is not a function TypeError: a is not a function
@User_3535 in service or controller?
@User_3535 I think you are messing with Dependency injection, pls check that
., thankz it worked ., i implemented in one controller and checking in other controller ., bad me .,
Ok, no problem.
0

Try to use Nprogress

JSFiddle

NProgress.start()

NProgress.done()

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.