1

Lets say I have some javascript as follows:

$('#bar').fileupload({
    baz: 2,
    baf: 6

    add: function(e, data){
       stop(e,data);
    },

    stop: function(e,data){
       console.log('I am Called');
    } 

});

From the add function i want to call the stop function in some situations. The problem is that now the stop function does not get called. How can I get this done?

Thanks

3
  • Maybe change your call to stop() to a call to stop(e, data) ? Commented Jul 15, 2015 at 9:48
  • this.stop(), but are you sure that is how the plugin is supposed to be used? When you define an anonymous function like this it is usually so as to configure the plugin, and not so that you can call it manually. Does the plugin have a website we can take a look at? Commented Jul 15, 2015 at 9:51
  • @RoyDictus I tried that but did't work. Commented Jul 15, 2015 at 9:51

3 Answers 3

2

The add method of fileuploader is a special case. Inside add, data.submit() returns a jQuery promise (https://api.jquery.com/jQuery.ajax/#jqXHR) that allows you to respond to different situations based on the status of the upload. This will let you hook into a custom function for different responses.

$('#bar').fileupload({
    baz: 2,
    baf: 6,
    add: function(e, data){
       data.submit()
       .success(function(result, status, promise) { console.log('I am Called'); });
    }
});

You also seem to be using the stop callback in an odd way. Fileuploader's stop option is a callback for when all file uploading has completed - I can't think of a situation where you would want to call that manually. If what you would like to do is prevent the upload, you'll need to look at the .abort() method as referenced in Parth Shah's answer.

 $('#bar').fileupload({
    add: function(e, data){
       data.submit()
       .error(function(promise, status, error) { promise.abort(); });
    }
});
Sign up to request clarification or add additional context in comments.

Comments

2

You're passing anonymous functions to fileupload, this means you have no way of referencing to them unless you declare them beforehand. Try declaring the two callbacks before with var, then you should be able to reference them

var stop = function(e,data){
    console.log('I am Called');
};

var add = function(e, data){
    stop();
};

$('#bar').fileupload({
    baz: 2,
    baf: 6, // you were missing a comma here
    add: add,
    stop: stop
});

Although I'm not sure why you would need to stop the add() function by calling another one, can't you just return from it?

EDIT: As others have pointed out, you shouldn't call the stop callback from add, my answer would be valid only in a more general case, please refer to the other answers or the documentation of fileupload to understand how to stop a download.

Comments

1

According to the documentation, here is how you should do it:

var jqXHR = null;
jqXHR = $('#bar').fileupload({
    ...,
    add: function (e, data) {
        jqXHR.abort();
    },
    ...
});

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.