How to cancel an upload? #290
Comments
I think I got it now. I wasn't using the add method at all, and I found out there is the place to get the jqXHR object (right?) |
Yes, invoking abort on a jqXHR object cancels the upload. |
I'm not getting this one working. var jqXHR; $('#fileupload').fileupload({ Thanks for your plugin! |
You can get access to the jqXHR object via the add callback (see API docs one before last section):
|
Thanks for replying so quick blueimp! I didn't use add because the file was being updated automatically and just used the other callbacks I needed. var jqXHR = null; $('#fileupload').fileupload({ add: function (e, data) { jqXHR = data.submit(); }, start: function (e) { $('#loader').show(); }, progress: function (e, data) { $.each(data.files, function (index, file) { var rate = (data.loaded / data.total) * 100 $('#prog_rate').text(Math.round(rate) + "% of " + (data.total/1000) + " KBytes ") }); }, done: function (e, data) { $.each(data.files, function (index, file) { $('#loader').hide(); $('#picture_input').hide(); $('#prog_rate').hide(); }); }, fail: function (e, data) { $.each(data.files, function (index, file) { $('#loader').hide(); $('#prog_rate').text("There was some error uploading the picture.") }); } }); $('#cancel_button').click(function (e) { jqXHR.abort(); }); Thanks a lot. |
I'm having some trouble with creating a custom abort button. Here's a video that explains: And here's my code:
|
Is there any way to cancel individually? When I abort the jqXHR it finishs all the process.. I need to cancel only current progress. How can i handle it? Thank you. |
To answer tevfik6 and for others' reference: _cancelHandler: function (e) {
e.preventDefault();
var template = $(e.currentTarget).closest('.template-upload'),
data = template.data('data') || {};
if (!data.jqXHR) {
data.errorThrown = 'abort';
this._trigger('fail', e, data);
} else {
data.jqXHR.abort();
}
}, Use this callback as a basis and when you create the add callback, make sure the jqXHR is included in the data variable on your data.context..(data.context.data('data', {}); ......data..data...data..data $('#fileupload').fileupload({
add: function(e, data) {
$('.progress_bar_wrapper').append($('.progress_context:first').clone());
data.context = $('.progress_context:last');
data.content.find('.abort').click(abortUpload );
var xhr = data.submit();
data.context.data('data',{jqXHR: xhr}); // so much data...
}
)};
function abortUpload (e) {
e.preventDefault();
var template = $(e.currentTarget).closest('.template-upload'),
data = template.data('data') || {}; // data, data , data (queue Monty Python skit)
if (!data.jqXHR) {
data.errorThrown = 'abort';
this._trigger('fail', e, data);
} else {
data.jqXHR.abort();
}
} I'm sure there is a better work around using sequentialUploads as well as more customized one since this doesn't fit the DRY methodology. Love the plug-in blueimp |
Thanks @bluetwin :) |
I tried to modify your code.
and my _cancel function is exactly the same as your abortUpload function. I have run it and I get to the data.jqXHR.abort(); call but my upload carries on. Any idea why? |
I'm not 100% sure, since I dont know the entire code, but it looks like the row element needs to be assigned to the data.context. add: function (e, data) {
var that = this;
var file = data.files[0];
var row = $('<tr class="template-upload" id="' + file.name.removeSpecialChars() + '"><td id="file-name">' + file.name + '</td><td id="file-size">' + file.size + '</td><td><div class="progress"><div class="bar" style="width:0%;"></div></div></td><td class="cancel"><button>Cancel</button></td></tr>');
row.appendTo('#files');
// attach new element to data context
data.context = row;
var jqXHR = $.getJSON('/Components/UploadHandler.ashx', { file: file.name }, function (result) {
var _file = result.file;
data.uploadedBytes = _file && _file.size;
$.blueimp.fileupload.prototype
.options.add.call(that, e, data);
});
row.data('data', { jqXHR: jqXHR }).find('.cancel').click(_cancel);
}, Also, just looking over the cancel class '.cancel' this is the class of the var row = $('<tr class="template-upload" id="' + file.name.removeSpecialChars() + '"><td id="file-name">' + file.name + '</td><td id="file-size">' + file.size + '</td><td><div class="progress"><div class="bar" style="width:0%;"></div></div></td><td><button class="cancel">Cancel</button></td></tr>'); I've been there with these roadblocks. |
@bluetwin Thanks a lot for your first comment showing us an example its been so useful for me |
I am using "basic" example upload plugin to upload file. I am using the example that is on this link. |
This is the way of how i can cancel all upload when you click a button: 1.First you have to bind the fileuploadadd event:
|
@carlostubff |
@phpdeveloperrahul glad to help you man, i was in your same position few months ago, but know i master this plugin haha i will publish project with a full demo implementation with all the features commented and explained |
Would fileupload('send').abort() work? |
@carlostubff Thank you :) |
I'm trying to use the basic plugin (not the UI version) in a project, but can't figure out how to cancel an ongoing upload. I tried several options, including one based on UI's _deleteHandler, but the XHR is never cancelled, according to Firebug. Could you please help? Thank you.
The text was updated successfully, but these errors were encountered: