Skip to content

Commit 1be873c

Browse files
committed
Added maxFileSize, minFileSize and acceptFileTypes options to the example implementation.
1 parent ef942f2 commit 1be873c

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

example/jquery.fileupload-uix.js

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery File Upload User Interface Extended Plugin 4.4.1
2+
* jQuery File Upload User Interface Extended Plugin 4.5
33
* https://github.com/blueimp/jQuery-File-Upload
44
*
55
* Copyright 2010, Sebastian Tschan
@@ -9,7 +9,7 @@
99
* http://creativecommons.org/licenses/MIT/
1010
*/
1111

12-
/*jslint regexp: false */
12+
/*jslint regexp: false, unparam: true */
1313
/*global jQuery */
1414

1515
(function ($) {
@@ -47,9 +47,13 @@
4747
UploadHandler = function (container, options) {
4848
var uploadHandler = this;
4949

50-
this.url = container.find('form:first').attr('action');
50+
this.locale = {};
51+
this.maxFileSize = 0;
52+
this.minFileSize = 1;
53+
this.acceptFileTypes = /.+$/i;
5154
this.autoUpload = false;
5255
this.forceIframeDownload = true;
56+
this.url = container.find('form:first').attr('action');
5357
this.dropZone = container.find('form:first');
5458
this.uploadTable = container.find('.files:first');
5559
this.downloadTable = this.uploadTable;
@@ -160,7 +164,7 @@
160164
.text(handler.formatFileSize(file.size));
161165
if (thumbnailUrl) {
162166
downloadRow.find('.file_download_preview').append(
163-
$('<a/>').append($('<img/>').attr('src', thumbnailUrl || null))
167+
$('<a/>').append($('<img/>').attr('src', thumbnailUrl))
164168
);
165169
downloadRow.find('a').attr('target', '_blank');
166170
}
@@ -172,13 +176,53 @@
172176
return downloadRow;
173177
};
174178

179+
this.uploadCallBack = function (event, files, index, xhr, handler, callBack) {
180+
callBack();
181+
};
182+
183+
this.onError = function (event, files, index, xhr, handler) {
184+
handler.uploadRow.addClass('file_upload_error')
185+
.find('.file_upload_progress').append($('<div class="error"/>').append(
186+
handler.locale[event] || event
187+
));
188+
};
189+
190+
this.validate = function (event, files, index, xhr, handler) {
191+
var isValid = true,
192+
file;
193+
if (typeof index !== 'number') {
194+
$.each(files, function (index, file) {
195+
isValid = handler.validate(event, files, index, xhr, handler);
196+
return isValid;
197+
});
198+
} else {
199+
file = files[index];
200+
if (handler.maxFileSize && file.size > handler.maxFileSize) {
201+
handler.onError('File is too big', files, index, xhr, handler);
202+
isValid = false;
203+
} else if (typeof file.size === 'number' && file.size < handler.minFileSize) {
204+
handler.onError('File is too small', files, index, xhr, handler);
205+
isValid = false;
206+
}
207+
if (!(handler.acceptFileTypes.test(file.type) ||
208+
handler.acceptFileTypes.test(file.name))) {
209+
handler.onError('Filetype not allowed', files, index, xhr, handler);
210+
isValid = false;
211+
}
212+
}
213+
return isValid;
214+
};
215+
175216
this.beforeSend = function (event, files, index, xhr, handler, callBack) {
217+
if (!handler.validate(event, files, index, xhr, handler)) {
218+
return;
219+
}
176220
if (handler.autoUpload) {
177-
callBack();
221+
handler.uploadCallBack(event, files, index, xhr, handler, callBack);
178222
} else {
179223
handler.uploadRow.find('.file_upload_start button').click(function (e) {
180224
$(this).fadeOut();
181-
callBack();
225+
handler.uploadCallBack(event, files, index, xhr, handler, callBack);
182226
e.preventDefault();
183227
});
184228
}

jquery.fileupload-ui.css

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@charset 'UTF-8';
22
/*
3-
* jQuery File Upload Plugin CSS 4.1
3+
* jQuery File Upload Plugin CSS 4.2
44
* https://github.com/blueimp/jQuery-File-Upload
55
*
66
* Copyright 2010, Sebastian Tschan
@@ -124,6 +124,7 @@ form.file_upload button,
124124
.file_upload .file_size {
125125
padding: 0 10px 0 0;
126126
text-align: right;
127+
white-space: nowrap;
127128
font-size: 1.1em;
128129
}
129130

@@ -132,6 +133,19 @@ form.file_upload button,
132133
margin: 10px 0;
133134
}
134135

136+
.file_upload_error .ui-progressbar,
137+
.file_upload_error .progressbar,
138+
.file_upload_error .file_upload_start button {
139+
display: none;
140+
}
141+
142+
.file_upload_error .error {
143+
width: 100%;
144+
height: 100%;
145+
color: #CD0A0A;
146+
font-size: 1.1em;
147+
}
148+
135149
.file_upload .ui-widget {
136150
font-size: 1em;
137151
}

0 commit comments

Comments
 (0)