10

here is how I use angular jquery file upload

var album = angular.module('album', ['restangular', 'blueimp.fileupload']),

.controller('somecontroller',function($scope,){
    $scope.options = {
        something
    }

 })

all I did was set the scope.options, change the controller ,and everything just magically works

setup the jquery file upload seems quite easy, but there are something really confuse me

how can I call the jquery file upload's callback function. for example, if the files uploaded successfully,I want to update the ui by calling fileuploaddone function ,it confuse me because there is no added file in my controller.

I'm new to angularJS, please help me to understand the workflow of angular jquery file upload

1 Answer 1

25

the blueimp.fileupload uses events that are fired via $emit to notify parent scopes:

             on([
                'fileuploadadd',
                'fileuploadsubmit',
                'fileuploadsend',
                'fileuploaddone',
                'fileuploadfail',
                'fileuploadalways',
                'fileuploadprogress',
                'fileuploadprogressall',
                'fileuploadstart',
                'fileuploadstop',
                'fileuploadchange',
                'fileuploadpaste',
                'fileuploaddrop',
                'fileuploaddragover',
                'fileuploadchunksend',
                'fileuploadchunkdone',
                'fileuploadchunkfail',
                'fileuploadchunkalways',
                'fileuploadprocessstart',
                'fileuploadprocess',
                'fileuploadprocessdone',
                'fileuploadprocessfail',
                'fileuploadprocessalways',
                'fileuploadprocessstop'
            ].join(' '), function (e, data) {
                if ($scope.$emit(e.type, data).defaultPrevented) {
                    e.preventDefault();
                }
            })

That means that you can simply add an event listener in one of the parent scope controllers, e.g.:

$scope.$on('fileuploadprocessdone', function(event, files){ 
    $.each(files, function (index, file) {
        //do what you want
    });
});

You can also override the default handleResponse function in your config phase, e.g.:

angular.module('myApp', ['blueimp.fileupload']).
.config(['fileUploadProvider', function (fileUploadProvider){
    fileUploadProvider.defaults.handleResponse = function (e,data){
        var files = data.result && data.result.files;
        if (files) {
            data.scope().replace(data.files, files);
            // do what you want...
        } else if (data.errorThrown || data.textStatus === 'error') {
             data.files[0].error = data.errorThrown ||
             data.textStatus;
        }
     };     
}]);
Sign up to request clarification or add additional context in comments.

3 Comments

+1 but Worth noting that if you use angular's $scope.$on() to bind (v1.1.5) as opposed to jQuery's $element.on() it does NOT currently accept a list of event names like the jquery version. IOW, you CANNOT do this $scope.$on(['idontwork', 'nor-me'].join(' ')], function(){}). Caught me out. See groups.google.com/forum/#!topic/angular/Ilv1uPOTxgY
take care that the second parameter from the event callback is the fileupload instance instead of an array with files. use function(event, fileuploader) { console.log(fileuploader.files); }) instead.
And what about fileuploadDESTROY?

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.