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