Skip to content

Commit 5723433

Browse files
committed
Added maxNumberOfFiles option.
1 parent ff9f99f commit 5723433

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

example/application.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery File Upload Plugin JS Example 4.4.1
2+
* jQuery File Upload Plugin JS Example 4.6
33
* https://github.com/blueimp/jQuery-File-Upload
44
*
55
* Copyright 2010, Sebastian Tschan
@@ -18,10 +18,11 @@ $(function () {
1818

1919
// Load existing files:
2020
$.getJSON($('#file_upload').fileUploadUIX('option', 'url'), function (files) {
21-
var fileUploadOptions = $('#file_upload').fileUploadUIX('option');
21+
var options = $('#file_upload').fileUploadUIX('option');
22+
options.adjustMaxNumberOfFiles(-files.length);
2223
$.each(files, function (index, file) {
23-
fileUploadOptions.buildDownloadRow(file, fileUploadOptions)
24-
.appendTo(fileUploadOptions.downloadTable).fadeIn();
24+
options.buildDownloadRow(file, options)
25+
.appendTo(options.downloadTable).fadeIn();
2526
});
2627
});
2728
});

example/jquery.fileupload-uix.js

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

5050
this.locale = {};
51-
this.maxFileSize = 0;
51+
this.maxFileSize = null;
5252
this.minFileSize = 1;
53+
this.maxNumberOfFiles = null;
5354
this.acceptFileTypes = /.+$/i;
5455
this.autoUpload = false;
5556
this.forceIframeDownload = true;
@@ -62,6 +63,13 @@
6263
this.downloadTemplate = this.uploadTable.find('.file_download_template:first');
6364
this.multiButtons = container.find('.file_upload_buttons:first');
6465

66+
this.adjustMaxNumberOfFiles = function (operand) {
67+
var number = container.fileUploadUIX('option', 'maxNumberOfFiles');
68+
if (typeof number === 'number') {
69+
container.fileUploadUIX('option', 'maxNumberOfFiles', number + operand);
70+
}
71+
};
72+
6573
this.formatFileSize = function (bytes) {
6674
if (typeof bytes !== 'number' || bytes === null) {
6775
return '';
@@ -176,10 +184,6 @@
176184
return downloadRow;
177185
};
178186

179-
this.uploadCallBack = function (event, files, index, xhr, handler, callBack) {
180-
callBack();
181-
};
182-
183187
this.onError = function (event, files, index, xhr, handler) {
184188
handler.uploadRow.addClass('file_upload_error')
185189
.find('.file_upload_progress').append($('<div class="error"/>').append(
@@ -193,7 +197,6 @@
193197
if (typeof index !== 'number') {
194198
$.each(files, function (index, file) {
195199
isValid = handler.validate(event, files, index, xhr, handler);
196-
return isValid;
197200
});
198201
} else {
199202
file = files[index];
@@ -209,14 +212,28 @@
209212
handler.onError('Filetype not allowed', files, index, xhr, handler);
210213
isValid = false;
211214
}
215+
if (typeof handler.maxNumberOfFiles === 'number' &&
216+
handler.maxNumberOfFiles < index + 1) {
217+
handler.onError('Max number exceeded', files, index, xhr, handler);
218+
isValid = false;
219+
}
212220
}
213221
return isValid;
214222
};
223+
224+
this.uploadCallBack = function (event, files, index, xhr, handler, callBack) {
225+
callBack();
226+
};
215227

216228
this.beforeSend = function (event, files, index, xhr, handler, callBack) {
217229
if (!handler.validate(event, files, index, xhr, handler)) {
218230
return;
219231
}
232+
var number = typeof index === 'number' ? 1 : files.length;
233+
handler.adjustMaxNumberOfFiles(-number);
234+
handler.uploadRow.find(handler.cancelSelector).click(function (e) {
235+
handler.adjustMaxNumberOfFiles(number);
236+
});
220237
if (handler.autoUpload) {
221238
handler.uploadCallBack(event, files, index, xhr, handler, callBack);
222239
} else {
@@ -246,6 +263,7 @@
246263
),
247264
type: 'DELETE',
248265
success: function () {
266+
uploadHandler.adjustMaxNumberOfFiles(1);
249267
row.fadeOut(function () {
250268
row.remove();
251269
});

0 commit comments

Comments
 (0)