Skip to content

Commit d504651

Browse files
committed
Added init & destroy options to the basic File Upload plugin.
Added initExtended & destroyExtended options to the File Upload UI plugin. Removed chunked upload defaults from the File Upload UIX version (example code), as Gecko 2.0 (Firefox etc.) adds blobs with an empty filename when building a multipart upload request using the FormData interface - see Bugzilla entry #649150: https://bugzilla.mozilla.org/show_bug.cgi?id=649150
1 parent bbf644e commit d504651

File tree

4 files changed

+70
-90
lines changed

4 files changed

+70
-90
lines changed

example/application.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery File Upload Plugin JS Example 4.4
2+
* jQuery File Upload Plugin JS Example 4.4.1
33
* https://github.com/blueimp/jQuery-File-Upload
44
*
55
* Copyright 2010, Sebastian Tschan
@@ -13,16 +13,7 @@
1313

1414
$(function () {
1515
// Initialize jQuery File Upload (Extended User Interface Version):
16-
$('#file_upload').fileUploadUIX({
17-
// Wait for user interaction before starting uploads:
18-
autoUpload: false,
19-
// Upload bigger files in chunks of 10 MB (remove or set to null to disable):
20-
maxChunkSize: 10000000,
21-
// Request uploaded filesize prior upload and upload remaining bytes:
22-
continueAbortedUploads: true,
23-
// Open download dialogs via iframes, to prevent aborting current uploads:
24-
forceIframeDownload: true
25-
});
16+
$('#file_upload').fileUploadUIX();
2617

2718
// Load existing files:
2819
$.getJSON($('#file_upload').fileUploadUIX('option', 'url'), function (files) {

example/jquery.fileupload-uix.js

Lines changed: 53 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery File Upload User Interface Extended Plugin 4.4
2+
* jQuery File Upload User Interface Extended Plugin 4.4.1
33
* https://github.com/blueimp/jQuery-File-Upload
44
*
55
* Copyright 2010, Sebastian Tschan
@@ -48,9 +48,8 @@
4848
var uploadHandler = this;
4949

5050
this.url = container.find('form:first').attr('action');
51-
this.autoUpload = true;
52-
this.continueAbortedUploads = false;
53-
this.forceIframeDownload = false;
51+
this.autoUpload = false;
52+
this.forceIframeDownload = true;
5453
this.dropZone = container.find('form:first');
5554
this.uploadTable = container.find('.files:first');
5655
this.downloadTable = this.uploadTable;
@@ -173,7 +172,7 @@
173172
return downloadRow;
174173
};
175174

176-
this.uploadCallBack = function (event, files, index, xhr, handler, callBack) {
175+
this.beforeSend = function (event, files, index, xhr, handler, callBack) {
177176
if (handler.autoUpload) {
178177
callBack();
179178
} else {
@@ -185,61 +184,49 @@
185184
}
186185
};
187186

188-
this.continueUploadCallBack = function (event, files, index, xhr, handler, callBack) {
189-
if (typeof index !== 'undefined') {
190-
$.getJSON(
191-
handler.url,
192-
{file: files[index].name},
193-
function (file) {
194-
if (file && file.size !== files[index].size) {
195-
handler.uploadedBytes = file.size;
196-
}
197-
handler.uploadCallBack(event, files, index, xhr, handler, callBack);
198-
}
199-
);
200-
} else {
201-
handler.uploadCallBack(event, files, index, xhr, handler, callBack);
202-
}
203-
};
204-
205-
this.beforeSend = function (event, files, index, xhr, handler, callBack) {
206-
if (handler.continueAbortedUploads) {
207-
handler.continueUploadCallBack(event, files, index, xhr, handler, callBack);
208-
} else {
209-
handler.uploadCallBack(event, files, index, xhr, handler, callBack);
187+
this.downloadHandler = function (e) {
188+
if (uploadHandler.forceIframeDownload) {
189+
// Open download dialogs via iframes, to prevent aborting current uploads:
190+
$('<iframe style="display:none;"/>')
191+
.attr('src', this.href)
192+
.appendTo(container);
193+
e.preventDefault();
210194
}
211195
};
212196

213-
this.initDownloadHandler = function () {
214-
if (uploadHandler.forceIframeDownload) {
215-
// Open download dialogs via iframes, to prevent aborting current uploads:
216-
uploadHandler.downloadTable.find('a:not([target="_blank"])')
217-
.live('click', function (e) {
218-
$('<iframe style="display:none;"/>')
219-
.attr('src', this.href)
220-
.appendTo(container);
221-
e.preventDefault();
197+
this.deleteHandler = function (e) {
198+
var row = $(this).closest('tr');
199+
$.ajax({
200+
url: uploadHandler.url + '?file=' + encodeURIComponent(
201+
row.attr('data-id')
202+
),
203+
type: 'DELETE',
204+
success: function () {
205+
row.fadeOut(function () {
206+
row.remove();
222207
});
223-
}
208+
}
209+
});
210+
e.preventDefault();
211+
};
212+
213+
this.initEventHandlers = function () {
214+
uploadHandler.downloadTable.find('a:not([target="_blank"])')
215+
.live('click', uploadHandler.downloadHandler);
216+
uploadHandler.downloadTable.find('.file_download_delete button')
217+
.live('click', uploadHandler.deleteHandler);
224218
};
225219

226-
this.initDeleteHandler = function () {
220+
this.destroyEventHandlers = function () {
221+
uploadHandler.downloadTable.find('a:not([target="_blank"])')
222+
.die('click', uploadHandler.downloadHandler);
227223
uploadHandler.downloadTable.find('.file_download_delete button')
228-
.live('click', function (e) {
229-
var row = $(this).closest('tr');
230-
$.ajax({
231-
url: uploadHandler.url + '?file=' + encodeURIComponent(
232-
row.attr('data-id')
233-
),
234-
type: 'DELETE',
235-
success: function () {
236-
row.fadeOut(function () {
237-
row.remove();
238-
});
239-
}
240-
});
241-
e.preventDefault();
242-
});
224+
.die('click', uploadHandler.deleteHandler);
225+
};
226+
227+
this.multiButtonHandler = function (e) {
228+
uploadHandler.uploadTable.find(e.data.selector + ' button:visible').click();
229+
e.preventDefault();
243230
};
244231

245232
this.initMultiButtons = function () {
@@ -248,36 +235,30 @@
248235
} else {
249236
uploadHandler.multiButtons.find('.file_upload_start:first')
250237
.button({icons: {primary: 'ui-icon-circle-arrow-e'}})
251-
.click(function (e) {
252-
uploadHandler.uploadTable.find('.file_upload_start button:visible').click();
253-
e.preventDefault();
254-
});
238+
.bind('click', {selector: '.file_upload_start'}, uploadHandler.multiButtonHandler);
255239
}
256240
uploadHandler.multiButtons.find('.file_upload_cancel:first')
257241
.button({icons: {primary: 'ui-icon-cancel'}})
258-
.click(function (e) {
259-
uploadHandler.uploadTable.find('.file_upload_cancel button:visible').click();
260-
e.preventDefault();
261-
});
242+
.bind('click', {selector: '.file_upload_cancel'}, uploadHandler.multiButtonHandler);
262243
uploadHandler.multiButtons.find('.file_download_delete:first')
263244
.button({icons: {primary: 'ui-icon-trash'}})
264-
.click(function (e) {
265-
uploadHandler.downloadTable.find('.file_download_delete button:visible').click();
266-
e.preventDefault();
267-
});
245+
.bind('click', {selector: '.file_download_delete'}, uploadHandler.multiButtonHandler);
246+
};
247+
248+
this.destroyMultiButtons = function () {
249+
uploadHandler.multiButtons.find(
250+
'.file_upload_start:first, .file_upload_cancel:first, .file_download_delete:first'
251+
).unbind('click', uploadHandler.multiButtonHandler).button('destroy').show();
268252
};
269253

270254
this.initExtended = function () {
271-
uploadHandler.initDownloadHandler();
272-
uploadHandler.initDeleteHandler();
255+
uploadHandler.initEventHandlers();
273256
uploadHandler.initMultiButtons();
274257
};
275258

276259
this.destroyExtended = function () {
277-
uploadHandler.downloadTable.find('.file_download_delete button').die('click');
278-
uploadHandler.multiButtons.find('.file_upload_start:first').button('destroy').show();
279-
uploadHandler.multiButtons.find('.file_upload_cancel:first').button('destroy');
280-
uploadHandler.multiButtons.find('.file_download_delete:first').button('destroy');
260+
uploadHandler.destroyEventHandlers();
261+
uploadHandler.destroyMultiButtons();
281262
};
282263

283264
$.extend(this, options);
@@ -286,8 +267,7 @@
286267
methods = {
287268
init : function (options) {
288269
return this.each(function () {
289-
$(this).fileUploadUI(new UploadHandler($(this), options))
290-
.fileUploadUI('option', 'initExtended', undefined, options.namespace)();
270+
$(this).fileUploadUI(new UploadHandler($(this), options));
291271
});
292272
},
293273

@@ -302,7 +282,6 @@
302282

303283
destroy : function (namespace) {
304284
return this.each(function () {
305-
$(this).fileUploadUI('option', 'destroyExtended', undefined, namespace)();
306285
$(this).fileUploadUI('destroy', namespace);
307286
});
308287
},

jquery.fileupload-ui.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery File Upload User Interface Plugin 4.3.1
2+
* jQuery File Upload User Interface Plugin 4.4
33
* https://github.com/blueimp/jQuery-File-Upload
44
*
55
* Copyright 2010, Sebastian Tschan
@@ -472,9 +472,15 @@
472472

473473
this.init = function () {
474474
uploadHandler.initProgressBarAll();
475+
if (typeof uploadHandler.initExtended === func) {
476+
uploadHandler.initExtended();
477+
}
475478
};
476479

477480
this.destroy = function () {
481+
if (typeof uploadHandler.destroyExtended === func) {
482+
uploadHandler.destroyExtended();
483+
}
478484
uploadHandler.destroyProgressBarAll();
479485
};
480486

@@ -484,8 +490,7 @@
484490
methods = {
485491
init : function (options) {
486492
return this.each(function () {
487-
$(this).fileUpload(new UploadHandler($(this), options))
488-
.fileUploadUI('option', 'init', undefined, options.namespace)();
493+
$(this).fileUpload(new UploadHandler($(this), options));
489494
});
490495
},
491496

@@ -500,7 +505,6 @@
500505

501506
destroy : function (namespace) {
502507
return this.each(function () {
503-
$(this).fileUploadUI('option', 'destroy', undefined, namespace)();
504508
$(this).fileUpload('destroy', namespace);
505509
});
506510
},

jquery.fileupload.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery File Upload Plugin 4.3.1
2+
* jQuery File Upload Plugin 4.4
33
* https://github.com/blueimp/jQuery-File-Upload
44
*
55
* Copyright 2010, Sebastian Tschan
@@ -790,6 +790,9 @@
790790
.addClass(settings.cssClass);
791791
settings.dropZone.not(container).addClass(settings.cssClass);
792792
initEventHandlers();
793+
if (typeof settings.init === func) {
794+
settings.init();
795+
}
793796
};
794797

795798
this.options = function (options) {
@@ -856,6 +859,9 @@
856859
};
857860

858861
this.destroy = function () {
862+
if (typeof settings.destroy === func) {
863+
settings.destroy();
864+
}
859865
removeEventHandlers();
860866
container
861867
.removeData(settings.namespace)

0 commit comments

Comments
 (0)