|
1
|
|
|
/* |
|
2
|
|
|
* jQuery File Upload Validation Plugin |
|
3
|
|
|
* https://github.com/blueimp/jQuery-File-Upload |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright 2013, Sebastian Tschan |
|
6
|
|
|
* https://blueimp.net |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under the MIT license: |
|
9
|
|
|
* https://opensource.org/licenses/MIT |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/* global define, require, window */ |
|
13
|
|
|
|
|
14
|
|
|
;(function (factory) { |
|
15
|
|
|
'use strict'; |
|
16
|
|
|
if (typeof define === 'function' && define.amd) { |
|
17
|
|
|
// Register as an anonymous AMD module: |
|
18
|
|
|
define([ |
|
19
|
|
|
'jquery', |
|
20
|
|
|
'./jquery.fileupload-process' |
|
21
|
|
|
], factory); |
|
22
|
|
|
} else if (typeof exports === 'object') { |
|
23
|
|
|
// Node/CommonJS: |
|
24
|
|
|
factory( |
|
25
|
|
|
require('jquery'), |
|
26
|
|
|
require('./jquery.fileupload-process') |
|
27
|
|
|
); |
|
28
|
|
|
} else { |
|
29
|
|
|
// Browser globals: |
|
30
|
|
|
factory( |
|
31
|
|
|
window.jQuery |
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
|
|
}(function ($) { |
|
35
|
|
|
'use strict'; |
|
36
|
|
|
|
|
37
|
|
|
// Append to the default processQueue: |
|
38
|
|
|
$.blueimp.fileupload.prototype.options.processQueue.push( |
|
39
|
|
|
{ |
|
40
|
|
|
action: 'validate', |
|
41
|
|
|
// Always trigger this action, |
|
42
|
|
|
// even if the previous action was rejected: |
|
43
|
|
|
always: true, |
|
44
|
|
|
// Options taken from the global options map: |
|
45
|
|
|
acceptFileTypes: '@', |
|
46
|
|
|
maxFileSize: '@', |
|
47
|
|
|
minFileSize: '@', |
|
48
|
|
|
maxNumberOfFiles: '@', |
|
49
|
|
|
disabled: '@disableValidation' |
|
50
|
|
|
} |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
// The File Upload Validation plugin extends the fileupload widget |
|
54
|
|
|
// with file validation functionality: |
|
55
|
|
|
$.widget('blueimp.fileupload', $.blueimp.fileupload, { |
|
56
|
|
|
|
|
57
|
|
|
options: { |
|
58
|
|
|
/* |
|
59
|
|
|
// The regular expression for allowed file types, matches |
|
60
|
|
|
// against either file type or file name: |
|
61
|
|
|
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i, |
|
62
|
|
|
// The maximum allowed file size in bytes: |
|
63
|
|
|
maxFileSize: 10000000, // 10 MB |
|
64
|
|
|
// The minimum allowed file size in bytes: |
|
65
|
|
|
minFileSize: undefined, // No minimal file size |
|
66
|
|
|
// The limit of files to be uploaded: |
|
67
|
|
|
maxNumberOfFiles: 10, |
|
68
|
|
|
*/ |
|
69
|
|
|
|
|
70
|
|
|
// Function returning the current number of files, |
|
71
|
|
|
// has to be overriden for maxNumberOfFiles validation: |
|
72
|
|
|
getNumberOfFiles: $.noop, |
|
73
|
|
|
|
|
74
|
|
|
// Error and info messages: |
|
75
|
|
|
messages: { |
|
76
|
|
|
maxNumberOfFiles: 'Maximum number of files exceeded', |
|
77
|
|
|
acceptFileTypes: 'File type not allowed', |
|
78
|
|
|
maxFileSize: 'File is too large', |
|
79
|
|
|
minFileSize: 'File is too small' |
|
80
|
|
|
} |
|
81
|
|
|
}, |
|
82
|
|
|
|
|
83
|
|
|
processActions: { |
|
84
|
|
|
|
|
85
|
|
|
validate: function (data, options) { |
|
86
|
|
|
if (options.disabled) { |
|
87
|
|
|
return data; |
|
88
|
|
|
} |
|
89
|
|
|
var dfd = $.Deferred(), |
|
90
|
|
|
settings = this.options, |
|
91
|
|
|
file = data.files[data.index], |
|
92
|
|
|
fileSize; |
|
93
|
|
|
if (options.minFileSize || options.maxFileSize) { |
|
94
|
|
|
fileSize = file.size; |
|
95
|
|
|
} |
|
96
|
|
|
if ($.type(options.maxNumberOfFiles) === 'number' && |
|
97
|
|
|
(settings.getNumberOfFiles() || 0) + data.files.length > |
|
98
|
|
|
options.maxNumberOfFiles) { |
|
99
|
|
|
file.error = settings.i18n('maxNumberOfFiles'); |
|
100
|
|
|
} else if (options.acceptFileTypes && |
|
101
|
|
|
!(options.acceptFileTypes.test(file.type) || |
|
102
|
|
|
options.acceptFileTypes.test(file.name))) { |
|
103
|
|
|
file.error = settings.i18n('acceptFileTypes'); |
|
104
|
|
|
} else if (fileSize > options.maxFileSize) { |
|
|
|
|
|
|
105
|
|
|
file.error = settings.i18n('maxFileSize'); |
|
106
|
|
|
} else if ($.type(fileSize) === 'number' && |
|
107
|
|
|
fileSize < options.minFileSize) { |
|
108
|
|
|
file.error = settings.i18n('minFileSize'); |
|
109
|
|
|
} else { |
|
110
|
|
|
delete file.error; |
|
111
|
|
|
} |
|
112
|
|
|
if (file.error || data.files.error) { |
|
113
|
|
|
data.files.error = true; |
|
114
|
|
|
dfd.rejectWith(this, [data]); |
|
115
|
|
|
} else { |
|
116
|
|
|
dfd.resolveWith(this, [data]); |
|
117
|
|
|
} |
|
118
|
|
|
return dfd.promise(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
}); |
|
124
|
|
|
|
|
125
|
|
|
})); |
|
126
|
|
|
|