| 9 |
lars |
1 |
/*
|
|
|
2 |
* jQuery File Upload Plugin 5.2
|
|
|
3 |
* https://github.com/blueimp/jQuery-File-Upload
|
|
|
4 |
*
|
|
|
5 |
* Copyright 2010, Sebastian Tschan
|
|
|
6 |
* https://blueimp.net
|
|
|
7 |
*
|
|
|
8 |
* Licensed under the MIT license:
|
|
|
9 |
* http://creativecommons.org/licenses/MIT/
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
/*jslint nomen: true, unparam: true, regexp: true */
|
|
|
13 |
/*global document, XMLHttpRequestUpload, Blob, File, FormData, location, jQuery */
|
|
|
14 |
|
|
|
15 |
(function ($) {
|
|
|
16 |
'use strict';
|
|
|
17 |
|
|
|
18 |
// The fileupload widget listens for change events on file input fields
|
|
|
19 |
// defined via fileInput setting and drop events of the given dropZone.
|
|
|
20 |
// In addition to the default jQuery Widget methods, the fileupload widget
|
|
|
21 |
// exposes the "add" and "send" methods, to add or directly send files
|
|
|
22 |
// using the fileupload API.
|
|
|
23 |
// By default, files added via file input selection, drag & drop or
|
|
|
24 |
// "add" method are uploaded immediately, but it is possible to override
|
|
|
25 |
// the "add" callback option to queue file uploads.
|
|
|
26 |
$.widget('blueimp.fileupload', {
|
|
|
27 |
|
|
|
28 |
options: {
|
|
|
29 |
// The namespace used for event handler binding on the dropZone and
|
|
|
30 |
// fileInput collections.
|
|
|
31 |
// If not set, the name of the widget ("fileupload") is used.
|
|
|
32 |
namespace: undefined,
|
|
|
33 |
// The drop target collection, by the default the complete document.
|
|
|
34 |
// Set to null or an empty collection to disable drag & drop support:
|
|
|
35 |
dropZone: $(document),
|
|
|
36 |
// The file input field collection, that is listened for change events.
|
|
|
37 |
// If undefined, it is set to the file input fields inside
|
|
|
38 |
// of the widget element on plugin initialization.
|
|
|
39 |
// Set to null or an empty collection to disable the change listener.
|
|
|
40 |
fileInput: undefined,
|
|
|
41 |
// By default, the file input field is replaced with a clone after
|
|
|
42 |
// each input field change event. This is required for iframe transport
|
|
|
43 |
// queues and allows change events to be fired for the same file
|
|
|
44 |
// selection, but can be disabled by setting the following option to false:
|
|
|
45 |
replaceFileInput: true,
|
|
|
46 |
// The parameter name for the file form data (the request argument name).
|
|
|
47 |
// If undefined or empty, the name property of the file input field is
|
|
|
48 |
// used, or "files[]" if the file input name property is also empty:
|
|
|
49 |
paramName: undefined,
|
|
|
50 |
// By default, each file of a selection is uploaded using an individual
|
|
|
51 |
// request for XHR type uploads. Set to false to upload file
|
|
|
52 |
// selections in one request each:
|
|
|
53 |
singleFileUploads: true,
|
|
|
54 |
// To limit the number of files uploaded with one XHR request,
|
|
|
55 |
// set the following option to an integer greater than 0:
|
|
|
56 |
limitMultiFileUploads: undefined,
|
|
|
57 |
// Set the following option to true to issue all file upload requests
|
|
|
58 |
// in a sequential order:
|
|
|
59 |
sequentialUploads: false,
|
|
|
60 |
// To limit the number of concurrent uploads,
|
|
|
61 |
// set the following option to an integer greater than 0:
|
|
|
62 |
limitConcurrentUploads: undefined,
|
|
|
63 |
// Set the following option to true to force iframe transport uploads:
|
|
|
64 |
forceIframeTransport: false,
|
|
|
65 |
// By default, XHR file uploads are sent as multipart/form-data.
|
|
|
66 |
// The iframe transport is always using multipart/form-data.
|
|
|
67 |
// Set to false to enable non-multipart XHR uploads:
|
|
|
68 |
multipart: true,
|
|
|
69 |
// To upload large files in smaller chunks, set the following option
|
|
|
70 |
// to a preferred maximum chunk size. If set to 0, null or undefined,
|
|
|
71 |
// or the browser does not support the required Blob API, files will
|
|
|
72 |
// be uploaded as a whole.
|
|
|
73 |
maxChunkSize: undefined,
|
|
|
74 |
// When a non-multipart upload or a chunked multipart upload has been
|
|
|
75 |
// aborted, this option can be used to resume the upload by setting
|
|
|
76 |
// it to the size of the already uploaded bytes. This option is most
|
|
|
77 |
// useful when modifying the options object inside of the "add" or
|
|
|
78 |
// "send" callbacks, as the options are cloned for each file upload.
|
|
|
79 |
uploadedBytes: undefined,
|
|
|
80 |
// By default, failed (abort or error) file uploads are removed from the
|
|
|
81 |
// global progress calculation. Set the following option to false to
|
|
|
82 |
// prevent recalculating the global progress data:
|
|
|
83 |
recalculateProgress: true,
|
|
|
84 |
|
|
|
85 |
// Additional form data to be sent along with the file uploads can be set
|
|
|
86 |
// using this option, which accepts an array of objects with name and
|
|
|
87 |
// value properties, a function returning such an array, a FormData
|
|
|
88 |
// object (for XHR file uploads), or a simple object.
|
|
|
89 |
// The form of the first fileInput is given as parameter to the function:
|
|
|
90 |
formData: function (form) {
|
|
|
91 |
return form.serializeArray();
|
|
|
92 |
},
|
|
|
93 |
|
|
|
94 |
// The add callback is invoked as soon as files are added to the fileupload
|
|
|
95 |
// widget (via file input selection, drag & drop or add API call).
|
|
|
96 |
// If the singleFileUploads option is enabled, this callback will be
|
|
|
97 |
// called once for each file in the selection for XHR file uplaods, else
|
|
|
98 |
// once for each file selection.
|
|
|
99 |
// The upload starts when the submit method is invoked on the data parameter.
|
|
|
100 |
// The data object contains a files property holding the added files
|
|
|
101 |
// and allows to override plugin options as well as define ajax settings.
|
|
|
102 |
// Listeners for this callback can also be bound the following way:
|
|
|
103 |
// .bind('fileuploadadd', func);
|
|
|
104 |
// data.submit() returns a Promise object and allows to attach additional
|
|
|
105 |
// handlers using jQuery's Deferred callbacks:
|
|
|
106 |
// data.submit().done(func).fail(func).always(func);
|
|
|
107 |
add: function (e, data) {
|
|
|
108 |
data.submit();
|
|
|
109 |
},
|
|
|
110 |
|
|
|
111 |
// Other callbacks:
|
|
|
112 |
// Callback for the start of each file upload request:
|
|
|
113 |
// send: function (e, data) {}, // .bind('fileuploadsend', func);
|
|
|
114 |
// Callback for successful uploads:
|
|
|
115 |
// done: function (e, data) {}, // .bind('fileuploaddone', func);
|
|
|
116 |
// Callback for failed (abort or error) uploads:
|
|
|
117 |
// fail: function (e, data) {}, // .bind('fileuploadfail', func);
|
|
|
118 |
// Callback for completed (success, abort or error) requests:
|
|
|
119 |
// always: function (e, data) {}, // .bind('fileuploadalways', func);
|
|
|
120 |
// Callback for upload progress events:
|
|
|
121 |
// progress: function (e, data) {}, // .bind('fileuploadprogress', func);
|
|
|
122 |
// Callback for global upload progress events:
|
|
|
123 |
// progressall: function (e, data) {}, // .bind('fileuploadprogressall', func);
|
|
|
124 |
// Callback for uploads start, equivalent to the global ajaxStart event:
|
|
|
125 |
// start: function (e) {}, // .bind('fileuploadstart', func);
|
|
|
126 |
// Callback for uploads stop, equivalent to the global ajaxStop event:
|
|
|
127 |
// stop: function (e) {}, // .bind('fileuploadstop', func);
|
|
|
128 |
// Callback for change events of the fileInput collection:
|
|
|
129 |
// change: function (e, data) {}, // .bind('fileuploadchange', func);
|
|
|
130 |
// Callback for drop events of the dropZone collection:
|
|
|
131 |
// drop: function (e, data) {}, // .bind('fileuploaddrop', func);
|
|
|
132 |
// Callback for dragover events of the dropZone collection:
|
|
|
133 |
// dragover: function (e) {}, // .bind('fileuploaddragover', func);
|
|
|
134 |
|
|
|
135 |
// The plugin options are used as settings object for the ajax calls.
|
|
|
136 |
// The following are jQuery ajax settings required for the file uploads:
|
|
|
137 |
processData: false,
|
|
|
138 |
contentType: false,
|
|
|
139 |
cache: false
|
|
|
140 |
},
|
|
|
141 |
|
|
|
142 |
// A list of options that require a refresh after assigning a new value:
|
|
|
143 |
_refreshOptionsList: ['namespace', 'dropZone', 'fileInput'],
|
|
|
144 |
|
|
|
145 |
_isXHRUpload: function (options) {
|
|
|
146 |
var undef = 'undefined';
|
|
|
147 |
return !options.forceIframeTransport &&
|
|
|
148 |
typeof XMLHttpRequestUpload !== undef && typeof File !== undef &&
|
|
|
149 |
(!options.multipart || typeof FormData !== undef);
|
|
|
150 |
},
|
|
|
151 |
|
|
|
152 |
_getFormData: function (options) {
|
|
|
153 |
var formData;
|
|
|
154 |
if (typeof options.formData === 'function') {
|
|
|
155 |
return options.formData(options.form);
|
|
|
156 |
} else if ($.isArray(options.formData)) {
|
|
|
157 |
return options.formData;
|
|
|
158 |
} else if (options.formData) {
|
|
|
159 |
formData = [];
|
|
|
160 |
$.each(options.formData, function (name, value) {
|
|
|
161 |
formData.push({name: name, value: value});
|
|
|
162 |
});
|
|
|
163 |
return formData;
|
|
|
164 |
}
|
|
|
165 |
return [];
|
|
|
166 |
},
|
|
|
167 |
|
|
|
168 |
_getTotal: function (files) {
|
|
|
169 |
var total = 0;
|
|
|
170 |
$.each(files, function (index, file) {
|
|
|
171 |
total += file.size || 1;
|
|
|
172 |
});
|
|
|
173 |
return total;
|
|
|
174 |
},
|
|
|
175 |
|
|
|
176 |
_onProgress: function (e, data) {
|
|
|
177 |
if (e.lengthComputable) {
|
|
|
178 |
var total = data.total || this._getTotal(data.files),
|
|
|
179 |
loaded = parseInt(
|
|
|
180 |
e.loaded / e.total * (data.chunkSize || total),
|
|
|
181 |
10
|
|
|
182 |
) + (data.uploadedBytes || 0);
|
|
|
183 |
this._loaded += loaded - (data.loaded || data.uploadedBytes || 0);
|
|
|
184 |
data.lengthComputable = true;
|
|
|
185 |
data.loaded = loaded;
|
|
|
186 |
data.total = total;
|
|
|
187 |
// Trigger a custom progress event with a total data property set
|
|
|
188 |
// to the file size(s) of the current upload and a loaded data
|
|
|
189 |
// property calculated accordingly:
|
|
|
190 |
this._trigger('progress', e, data);
|
|
|
191 |
// Trigger a global progress event for all current file uploads,
|
|
|
192 |
// including ajax calls queued for sequential file uploads:
|
|
|
193 |
this._trigger('progressall', e, {
|
|
|
194 |
lengthComputable: true,
|
|
|
195 |
loaded: this._loaded,
|
|
|
196 |
total: this._total
|
|
|
197 |
});
|
|
|
198 |
}
|
|
|
199 |
},
|
|
|
200 |
|
|
|
201 |
_initProgressListener: function (options) {
|
|
|
202 |
var that = this,
|
|
|
203 |
xhr = options.xhr ? options.xhr() : $.ajaxSettings.xhr();
|
|
|
204 |
// Accesss to the native XHR object is required to add event listeners
|
|
|
205 |
// for the upload progress event:
|
|
|
206 |
if (xhr.upload && xhr.upload.addEventListener) {
|
|
|
207 |
xhr.upload.addEventListener('progress', function (e) {
|
|
|
208 |
that._onProgress(e, options);
|
|
|
209 |
}, false);
|
|
|
210 |
options.xhr = function () {
|
|
|
211 |
return xhr;
|
|
|
212 |
};
|
|
|
213 |
}
|
|
|
214 |
},
|
|
|
215 |
|
|
|
216 |
_initXHRData: function (options) {
|
|
|
217 |
var formData,
|
|
|
218 |
file = options.files[0];
|
|
|
219 |
if (!options.multipart || options.blob) {
|
|
|
220 |
// For non-multipart uploads and chunked uploads,
|
|
|
221 |
// file meta data is not part of the request body,
|
|
|
222 |
// so we transmit this data as part of the HTTP headers.
|
|
|
223 |
// For cross domain requests, these headers must be allowed
|
|
|
224 |
// via Access-Control-Allow-Headers or removed using
|
|
|
225 |
// the beforeSend callback:
|
|
|
226 |
options.headers = $.extend(options.headers, {
|
|
|
227 |
'X-File-Name': file.name,
|
|
|
228 |
'X-File-Type': file.type,
|
|
|
229 |
'X-File-Size': file.size
|
|
|
230 |
});
|
|
|
231 |
if (!options.blob) {
|
|
|
232 |
// Non-chunked non-multipart upload:
|
|
|
233 |
options.contentType = file.type;
|
|
|
234 |
options.data = file;
|
|
|
235 |
} else if (!options.multipart) {
|
|
|
236 |
// Chunked non-multipart upload:
|
|
|
237 |
options.contentType = 'application/octet-stream';
|
|
|
238 |
options.data = options.blob;
|
|
|
239 |
}
|
|
|
240 |
}
|
|
|
241 |
if (options.multipart && typeof FormData !== 'undefined') {
|
|
|
242 |
if (options.formData instanceof FormData) {
|
|
|
243 |
formData = options.formData;
|
|
|
244 |
} else {
|
|
|
245 |
formData = new FormData();
|
|
|
246 |
$.each(this._getFormData(options), function (index, field) {
|
|
|
247 |
formData.append(field.name, field.value);
|
|
|
248 |
});
|
|
|
249 |
}
|
|
|
250 |
if (options.blob) {
|
|
|
251 |
formData.append(options.paramName, options.blob);
|
|
|
252 |
} else {
|
|
|
253 |
$.each(options.files, function (index, file) {
|
|
|
254 |
// File objects are also Blob instances.
|
|
|
255 |
// This check allows the tests to run with
|
|
|
256 |
// dummy objects:
|
|
|
257 |
if (file instanceof Blob) {
|
|
|
258 |
formData.append(options.paramName, file);
|
|
|
259 |
}
|
|
|
260 |
});
|
|
|
261 |
}
|
|
|
262 |
options.data = formData;
|
|
|
263 |
}
|
|
|
264 |
// Blob reference is not needed anymore, free memory:
|
|
|
265 |
options.blob = null;
|
|
|
266 |
},
|
|
|
267 |
|
|
|
268 |
_initIframeSettings: function (options) {
|
|
|
269 |
// Setting the dataType to iframe enables the iframe transport:
|
|
|
270 |
options.dataType = 'iframe ' + (options.dataType || '');
|
|
|
271 |
// The iframe transport accepts a serialized array as form data:
|
|
|
272 |
options.formData = this._getFormData(options);
|
|
|
273 |
},
|
|
|
274 |
|
|
|
275 |
_initDataSettings: function (options) {
|
|
|
276 |
if (this._isXHRUpload(options)) {
|
|
|
277 |
if (!this._chunkedUpload(options, true)) {
|
|
|
278 |
if (!options.data) {
|
|
|
279 |
this._initXHRData(options);
|
|
|
280 |
}
|
|
|
281 |
this._initProgressListener(options);
|
|
|
282 |
}
|
|
|
283 |
} else {
|
|
|
284 |
this._initIframeSettings(options);
|
|
|
285 |
}
|
|
|
286 |
},
|
|
|
287 |
|
|
|
288 |
_initFormSettings: function (options) {
|
|
|
289 |
// Retrieve missing options from the input field and the
|
|
|
290 |
// associated form, if available:
|
|
|
291 |
if (!options.form || !options.form.length) {
|
|
|
292 |
options.form = $(options.fileInput.prop('form'));
|
|
|
293 |
}
|
|
|
294 |
if (!options.paramName) {
|
|
|
295 |
options.paramName = options.fileInput.prop('name') ||
|
|
|
296 |
'files[]';
|
|
|
297 |
}
|
|
|
298 |
if (!options.url) {
|
|
|
299 |
options.url = options.form.prop('action') || location.href;
|
|
|
300 |
}
|
|
|
301 |
// The HTTP request method must be "POST" or "PUT":
|
|
|
302 |
options.type = (options.type || options.form.prop('method') || '')
|
|
|
303 |
.toUpperCase();
|
|
|
304 |
if (options.type !== 'POST' && options.type !== 'PUT') {
|
|
|
305 |
options.type = 'POST';
|
|
|
306 |
}
|
|
|
307 |
},
|
|
|
308 |
|
|
|
309 |
_getAJAXSettings: function (data) {
|
|
|
310 |
var options = $.extend({}, this.options, data);
|
|
|
311 |
this._initFormSettings(options);
|
|
|
312 |
this._initDataSettings(options);
|
|
|
313 |
return options;
|
|
|
314 |
},
|
|
|
315 |
|
|
|
316 |
// Maps jqXHR callbacks to the equivalent
|
|
|
317 |
// methods of the given Promise object:
|
|
|
318 |
_enhancePromise: function (promise) {
|
|
|
319 |
promise.success = promise.done;
|
|
|
320 |
promise.error = promise.fail;
|
|
|
321 |
promise.complete = promise.always;
|
|
|
322 |
return promise;
|
|
|
323 |
},
|
|
|
324 |
|
|
|
325 |
// Creates and returns a Promise object enhanced with
|
|
|
326 |
// the jqXHR methods abort, success, error and complete:
|
|
|
327 |
_getXHRPromise: function (resolveOrReject, context, args) {
|
|
|
328 |
var dfd = $.Deferred(),
|
|
|
329 |
promise = dfd.promise();
|
|
|
330 |
context = context || this.options.context || promise;
|
|
|
331 |
if (resolveOrReject === true) {
|
|
|
332 |
dfd.resolveWith(context, args);
|
|
|
333 |
} else if (resolveOrReject === false) {
|
|
|
334 |
dfd.rejectWith(context, args);
|
|
|
335 |
}
|
|
|
336 |
promise.abort = dfd.promise;
|
|
|
337 |
return this._enhancePromise(promise);
|
|
|
338 |
},
|
|
|
339 |
|
|
|
340 |
// Uploads a file in multiple, sequential requests
|
|
|
341 |
// by splitting the file up in multiple blob chunks.
|
|
|
342 |
// If the second parameter is true, only tests if the file
|
|
|
343 |
// should be uploaded in chunks, but does not invoke any
|
|
|
344 |
// upload requests:
|
|
|
345 |
_chunkedUpload: function (options, testOnly) {
|
|
|
346 |
var that = this,
|
|
|
347 |
file = options.files[0],
|
|
|
348 |
fs = file.size,
|
|
|
349 |
ub = options.uploadedBytes = options.uploadedBytes || 0,
|
|
|
350 |
mcs = options.maxChunkSize || fs,
|
|
|
351 |
// Use the Blob methods with the slice implementation
|
|
|
352 |
// according to the W3C Blob API specification:
|
|
|
353 |
slice = file.webkitSlice || file.mozSlice || file.slice,
|
|
|
354 |
upload,
|
|
|
355 |
n,
|
|
|
356 |
jqXHR,
|
|
|
357 |
pipe;
|
|
|
358 |
if (!(this._isXHRUpload(options) && slice && (ub || mcs < fs)) ||
|
|
|
359 |
options.data) {
|
|
|
360 |
return false;
|
|
|
361 |
}
|
|
|
362 |
if (testOnly) {
|
|
|
363 |
return true;
|
|
|
364 |
}
|
|
|
365 |
if (ub >= fs) {
|
|
|
366 |
file.error = 'uploadedBytes';
|
|
|
367 |
return this._getXHRPromise(false);
|
|
|
368 |
}
|
|
|
369 |
// n is the number of blobs to upload,
|
|
|
370 |
// calculated via filesize, uploaded bytes and max chunk size:
|
|
|
371 |
n = Math.ceil((fs - ub) / mcs);
|
|
|
372 |
// The chunk upload method accepting the chunk number as parameter:
|
|
|
373 |
upload = function (i) {
|
|
|
374 |
if (!i) {
|
|
|
375 |
return that._getXHRPromise(true);
|
|
|
376 |
}
|
|
|
377 |
// Upload the blobs in sequential order:
|
|
|
378 |
return upload(i -= 1).pipe(function () {
|
|
|
379 |
// Clone the options object for each chunk upload:
|
|
|
380 |
var o = $.extend({}, options);
|
|
|
381 |
o.blob = slice.call(
|
|
|
382 |
file,
|
|
|
383 |
ub + i * mcs,
|
|
|
384 |
ub + (i + 1) * mcs
|
|
|
385 |
);
|
|
|
386 |
// Store the current chunk size, as the blob itself
|
|
|
387 |
// will be dereferenced after data processing:
|
|
|
388 |
o.chunkSize = o.blob.size;
|
|
|
389 |
// Process the upload data (the blob and potential form data):
|
|
|
390 |
that._initXHRData(o);
|
|
|
391 |
// Add progress listeners for this chunk upload:
|
|
|
392 |
that._initProgressListener(o);
|
|
|
393 |
jqXHR = ($.ajax(o) || that._getXHRPromise(false, o.context))
|
|
|
394 |
.done(function () {
|
|
|
395 |
// Create a progress event if upload is done and
|
|
|
396 |
// no progress event has been invoked for this chunk:
|
|
|
397 |
if (!o.loaded) {
|
|
|
398 |
that._onProgress($.Event('progress', {
|
|
|
399 |
lengthComputable: true,
|
|
|
400 |
loaded: o.chunkSize,
|
|
|
401 |
total: o.chunkSize
|
|
|
402 |
}), o);
|
|
|
403 |
}
|
|
|
404 |
options.uploadedBytes = o.uploadedBytes
|
|
|
405 |
+= o.chunkSize;
|
|
|
406 |
});
|
|
|
407 |
return jqXHR;
|
|
|
408 |
});
|
|
|
409 |
};
|
|
|
410 |
// Return the piped Promise object, enhanced with an abort method,
|
|
|
411 |
// which is delegated to the jqXHR object of the current upload,
|
|
|
412 |
// and jqXHR callbacks mapped to the equivalent Promise methods:
|
|
|
413 |
pipe = upload(n);
|
|
|
414 |
pipe.abort = function () {
|
|
|
415 |
return jqXHR.abort();
|
|
|
416 |
};
|
|
|
417 |
return this._enhancePromise(pipe);
|
|
|
418 |
},
|
|
|
419 |
|
|
|
420 |
_beforeSend: function (e, data) {
|
|
|
421 |
if (this._active === 0) {
|
|
|
422 |
// the start callback is triggered when an upload starts
|
|
|
423 |
// and no other uploads are currently running,
|
|
|
424 |
// equivalent to the global ajaxStart event:
|
|
|
425 |
this._trigger('start');
|
|
|
426 |
}
|
|
|
427 |
this._active += 1;
|
|
|
428 |
// Initialize the global progress values:
|
|
|
429 |
this._loaded += data.uploadedBytes || 0;
|
|
|
430 |
this._total += this._getTotal(data.files);
|
|
|
431 |
},
|
|
|
432 |
|
|
|
433 |
_onDone: function (result, textStatus, jqXHR, options) {
|
|
|
434 |
if (!this._isXHRUpload(options)) {
|
|
|
435 |
// Create a progress event for each iframe load:
|
|
|
436 |
this._onProgress($.Event('progress', {
|
|
|
437 |
lengthComputable: true,
|
|
|
438 |
loaded: 1,
|
|
|
439 |
total: 1
|
|
|
440 |
}), options);
|
|
|
441 |
}
|
|
|
442 |
options.result = result;
|
|
|
443 |
options.textStatus = textStatus;
|
|
|
444 |
options.jqXHR = jqXHR;
|
|
|
445 |
this._trigger('done', null, options);
|
|
|
446 |
},
|
|
|
447 |
|
|
|
448 |
_onFail: function (jqXHR, textStatus, errorThrown, options) {
|
|
|
449 |
options.jqXHR = jqXHR;
|
|
|
450 |
options.textStatus = textStatus;
|
|
|
451 |
options.errorThrown = errorThrown;
|
|
|
452 |
this._trigger('fail', null, options);
|
|
|
453 |
if (options.recalculateProgress) {
|
|
|
454 |
// Remove the failed (error or abort) file upload from
|
|
|
455 |
// the global progress calculation:
|
|
|
456 |
this._loaded -= options.loaded || options.uploadedBytes || 0;
|
|
|
457 |
this._total -= options.total || this._getTotal(options.files);
|
|
|
458 |
}
|
|
|
459 |
},
|
|
|
460 |
|
|
|
461 |
_onAlways: function (result, textStatus, jqXHR, errorThrown, options) {
|
|
|
462 |
this._active -= 1;
|
|
|
463 |
options.result = result;
|
|
|
464 |
options.textStatus = textStatus;
|
|
|
465 |
options.jqXHR = jqXHR;
|
|
|
466 |
options.errorThrown = errorThrown;
|
|
|
467 |
this._trigger('always', null, options);
|
|
|
468 |
if (this._active === 0) {
|
|
|
469 |
// The stop callback is triggered when all uploads have
|
|
|
470 |
// been completed, equivalent to the global ajaxStop event:
|
|
|
471 |
this._trigger('stop');
|
|
|
472 |
// Reset the global progress values:
|
|
|
473 |
this._loaded = this._total = 0;
|
|
|
474 |
}
|
|
|
475 |
},
|
|
|
476 |
|
|
|
477 |
_onSend: function (e, data) {
|
|
|
478 |
var that = this,
|
|
|
479 |
jqXHR,
|
|
|
480 |
slot,
|
|
|
481 |
pipe,
|
|
|
482 |
options = that._getAJAXSettings(data),
|
|
|
483 |
send = function (resolve, args) {
|
|
|
484 |
that._sending += 1;
|
|
|
485 |
jqXHR = jqXHR || (
|
|
|
486 |
(resolve !== false &&
|
|
|
487 |
that._trigger('send', e, options) !== false &&
|
|
|
488 |
(that._chunkedUpload(options) || $.ajax(options))) ||
|
|
|
489 |
that._getXHRPromise(false, options.context, args)
|
|
|
490 |
).done(function (result, textStatus, jqXHR) {
|
|
|
491 |
that._onDone(result, textStatus, jqXHR, options);
|
|
|
492 |
}).fail(function (jqXHR, textStatus, errorThrown) {
|
|
|
493 |
that._onFail(jqXHR, textStatus, errorThrown, options);
|
|
|
494 |
}).always(function (a1, a2, a3) {
|
|
|
495 |
that._sending -= 1;
|
|
|
496 |
if (a3 && a3.done) {
|
|
|
497 |
that._onAlways(a1, a2, a3, undefined, options);
|
|
|
498 |
} else {
|
|
|
499 |
that._onAlways(undefined, a2, a1, a3, options);
|
|
|
500 |
}
|
|
|
501 |
if (options.limitConcurrentUploads &&
|
|
|
502 |
options.limitConcurrentUploads > that._sending) {
|
|
|
503 |
// Start the next queued upload,
|
|
|
504 |
// that has not been aborted:
|
|
|
505 |
var nextSlot = that._slots.shift();
|
|
|
506 |
while (nextSlot) {
|
|
|
507 |
if (!nextSlot.isRejected()) {
|
|
|
508 |
nextSlot.resolve();
|
|
|
509 |
break;
|
|
|
510 |
}
|
|
|
511 |
nextSlot = that._slots.shift();
|
|
|
512 |
}
|
|
|
513 |
}
|
|
|
514 |
});
|
|
|
515 |
return jqXHR;
|
|
|
516 |
};
|
|
|
517 |
this._beforeSend(e, options);
|
|
|
518 |
if (this.options.sequentialUploads ||
|
|
|
519 |
(this.options.limitConcurrentUploads &&
|
|
|
520 |
this.options.limitConcurrentUploads <= this._sending)) {
|
|
|
521 |
if (this.options.limitConcurrentUploads > 1) {
|
|
|
522 |
slot = $.Deferred();
|
|
|
523 |
this._slots.push(slot);
|
|
|
524 |
pipe = slot.pipe(send);
|
|
|
525 |
} else {
|
|
|
526 |
pipe = (this._sequence = this._sequence.pipe(send, send));
|
|
|
527 |
}
|
|
|
528 |
// Return the piped Promise object, enhanced with an abort method,
|
|
|
529 |
// which is delegated to the jqXHR object of the current upload,
|
|
|
530 |
// and jqXHR callbacks mapped to the equivalent Promise methods:
|
|
|
531 |
pipe.abort = function () {
|
|
|
532 |
var args = [undefined, 'abort', 'abort'];
|
|
|
533 |
if (!jqXHR) {
|
|
|
534 |
if (slot) {
|
|
|
535 |
slot.rejectWith(args);
|
|
|
536 |
}
|
|
|
537 |
return send(false, args);
|
|
|
538 |
}
|
|
|
539 |
return jqXHR.abort();
|
|
|
540 |
};
|
|
|
541 |
return this._enhancePromise(pipe);
|
|
|
542 |
}
|
|
|
543 |
return send();
|
|
|
544 |
},
|
|
|
545 |
|
|
|
546 |
_onAdd: function (e, data) {
|
|
|
547 |
var that = this,
|
|
|
548 |
result = true,
|
|
|
549 |
options = $.extend({}, this.options, data),
|
|
|
550 |
fileSet = data.files,
|
|
|
551 |
limit = options.limitMultiFileUploads,
|
|
|
552 |
i;
|
|
|
553 |
if (!(options.singleFileUploads || limit) ||
|
|
|
554 |
!this._isXHRUpload(options)) {
|
|
|
555 |
fileSet = [fileSet];
|
|
|
556 |
} else if (!options.singleFileUploads && limit) {
|
|
|
557 |
fileSet = [];
|
|
|
558 |
for (i = 0; i < data.files.length; i += limit) {
|
|
|
559 |
fileSet.push(data.files.slice(i, i + limit));
|
|
|
560 |
}
|
|
|
561 |
}
|
|
|
562 |
$.each(fileSet, function (index, file) {
|
|
|
563 |
var files = $.isArray(file) ? file : [file],
|
|
|
564 |
newData = $.extend({}, data, {files: files});
|
|
|
565 |
newData.submit = function () {
|
|
|
566 |
return that._onSend(e, newData);
|
|
|
567 |
};
|
|
|
568 |
return (result = that._trigger('add', e, newData));
|
|
|
569 |
});
|
|
|
570 |
return result;
|
|
|
571 |
},
|
|
|
572 |
|
|
|
573 |
// File Normalization for Gecko 1.9.1 (Firefox 3.5) support:
|
|
|
574 |
_normalizeFile: function (index, file) {
|
|
|
575 |
if (file.name === undefined && file.size === undefined) {
|
|
|
576 |
file.name = file.fileName;
|
|
|
577 |
file.size = file.fileSize;
|
|
|
578 |
}
|
|
|
579 |
},
|
|
|
580 |
|
|
|
581 |
_replaceFileInput: function (input) {
|
|
|
582 |
var inputClone = input.clone(true);
|
|
|
583 |
$('<form></form>').append(inputClone)[0].reset();
|
|
|
584 |
// Detaching allows to insert the fileInput on another form
|
|
|
585 |
// without loosing the file input value:
|
|
|
586 |
input.after(inputClone).detach();
|
|
|
587 |
// Replace the original file input element in the fileInput
|
|
|
588 |
// collection with the clone, which has been copied including
|
|
|
589 |
// event handlers:
|
|
|
590 |
this.options.fileInput = this.options.fileInput.map(function (i, el) {
|
|
|
591 |
if (el === input[0]) {
|
|
|
592 |
return inputClone[0];
|
|
|
593 |
}
|
|
|
594 |
return el;
|
|
|
595 |
});
|
|
|
596 |
},
|
|
|
597 |
|
|
|
598 |
_onChange: function (e) {
|
|
|
599 |
var that = e.data.fileupload,
|
|
|
600 |
data = {
|
|
|
601 |
files: $.each($.makeArray(e.target.files), that._normalizeFile),
|
|
|
602 |
fileInput: $(e.target),
|
|
|
603 |
form: $(e.target.form)
|
|
|
604 |
};
|
|
|
605 |
if (!data.files.length) {
|
|
|
606 |
// If the files property is not available, the browser does not
|
|
|
607 |
// support the File API and we add a pseudo File object with
|
|
|
608 |
// the input value as name with path information removed:
|
|
|
609 |
data.files = [{name: e.target.value.replace(/^.*\\/, '')}];
|
|
|
610 |
}
|
|
|
611 |
// Store the form reference as jQuery data for other event handlers,
|
|
|
612 |
// as the form property is not available after replacing the file input:
|
|
|
613 |
if (data.form.length) {
|
|
|
614 |
data.fileInput.data('blueimp.fileupload.form', data.form);
|
|
|
615 |
} else {
|
|
|
616 |
data.form = data.fileInput.data('blueimp.fileupload.form');
|
|
|
617 |
}
|
|
|
618 |
if (that.options.replaceFileInput) {
|
|
|
619 |
that._replaceFileInput(data.fileInput);
|
|
|
620 |
}
|
|
|
621 |
if (that._trigger('change', e, data) === false ||
|
|
|
622 |
that._onAdd(e, data) === false) {
|
|
|
623 |
return false;
|
|
|
624 |
}
|
|
|
625 |
},
|
|
|
626 |
|
|
|
627 |
_onDrop: function (e) {
|
|
|
628 |
var that = e.data.fileupload,
|
|
|
629 |
dataTransfer = e.dataTransfer = e.originalEvent.dataTransfer,
|
|
|
630 |
data = {
|
|
|
631 |
files: $.each(
|
|
|
632 |
$.makeArray(dataTransfer && dataTransfer.files),
|
|
|
633 |
that._normalizeFile
|
|
|
634 |
)
|
|
|
635 |
};
|
|
|
636 |
if (that._trigger('drop', e, data) === false ||
|
|
|
637 |
that._onAdd(e, data) === false) {
|
|
|
638 |
return false;
|
|
|
639 |
}
|
|
|
640 |
e.preventDefault();
|
|
|
641 |
},
|
|
|
642 |
|
|
|
643 |
_onDragOver: function (e) {
|
|
|
644 |
var that = e.data.fileupload,
|
|
|
645 |
dataTransfer = e.dataTransfer = e.originalEvent.dataTransfer;
|
|
|
646 |
if (that._trigger('dragover', e) === false) {
|
|
|
647 |
return false;
|
|
|
648 |
}
|
|
|
649 |
if (dataTransfer) {
|
|
|
650 |
dataTransfer.dropEffect = dataTransfer.effectAllowed = 'copy';
|
|
|
651 |
}
|
|
|
652 |
e.preventDefault();
|
|
|
653 |
},
|
|
|
654 |
|
|
|
655 |
_initEventHandlers: function () {
|
|
|
656 |
var ns = this.options.namespace || this.name;
|
|
|
657 |
this.options.dropZone
|
|
|
658 |
.bind('dragover.' + ns, {fileupload: this}, this._onDragOver)
|
|
|
659 |
.bind('drop.' + ns, {fileupload: this}, this._onDrop);
|
|
|
660 |
this.options.fileInput
|
|
|
661 |
.bind('change.' + ns, {fileupload: this}, this._onChange);
|
|
|
662 |
},
|
|
|
663 |
|
|
|
664 |
_destroyEventHandlers: function () {
|
|
|
665 |
var ns = this.options.namespace || this.name;
|
|
|
666 |
this.options.dropZone
|
|
|
667 |
.unbind('dragover.' + ns, this._onDragOver)
|
|
|
668 |
.unbind('drop.' + ns, this._onDrop);
|
|
|
669 |
this.options.fileInput
|
|
|
670 |
.unbind('change.' + ns, this._onChange);
|
|
|
671 |
},
|
|
|
672 |
|
|
|
673 |
_beforeSetOption: function (key, value) {
|
|
|
674 |
this._destroyEventHandlers();
|
|
|
675 |
},
|
|
|
676 |
|
|
|
677 |
_afterSetOption: function (key, value) {
|
|
|
678 |
var options = this.options;
|
|
|
679 |
if (!options.fileInput) {
|
|
|
680 |
options.fileInput = $();
|
|
|
681 |
}
|
|
|
682 |
if (!options.dropZone) {
|
|
|
683 |
options.dropZone = $();
|
|
|
684 |
}
|
|
|
685 |
this._initEventHandlers();
|
|
|
686 |
},
|
|
|
687 |
|
|
|
688 |
_setOption: function (key, value) {
|
|
|
689 |
var refresh = $.inArray(key, this._refreshOptionsList) !== -1;
|
|
|
690 |
if (refresh) {
|
|
|
691 |
this._beforeSetOption(key, value);
|
|
|
692 |
}
|
|
|
693 |
$.Widget.prototype._setOption.call(this, key, value);
|
|
|
694 |
if (refresh) {
|
|
|
695 |
this._afterSetOption(key, value);
|
|
|
696 |
}
|
|
|
697 |
},
|
|
|
698 |
|
|
|
699 |
_create: function () {
|
|
|
700 |
var options = this.options;
|
|
|
701 |
if (options.fileInput === undefined) {
|
|
|
702 |
options.fileInput = this.element.is('input:file') ?
|
|
|
703 |
this.element : this.element.find('input:file');
|
|
|
704 |
} else if (!options.fileInput) {
|
|
|
705 |
options.fileInput = $();
|
|
|
706 |
}
|
|
|
707 |
if (!options.dropZone) {
|
|
|
708 |
options.dropZone = $();
|
|
|
709 |
}
|
|
|
710 |
this._slots = [];
|
|
|
711 |
this._sequence = this._getXHRPromise(true);
|
|
|
712 |
this._sending = this._active = this._loaded = this._total = 0;
|
|
|
713 |
this._initEventHandlers();
|
|
|
714 |
},
|
|
|
715 |
|
|
|
716 |
destroy: function () {
|
|
|
717 |
this._destroyEventHandlers();
|
|
|
718 |
$.Widget.prototype.destroy.call(this);
|
|
|
719 |
},
|
|
|
720 |
|
|
|
721 |
enable: function () {
|
|
|
722 |
$.Widget.prototype.enable.call(this);
|
|
|
723 |
this._initEventHandlers();
|
|
|
724 |
},
|
|
|
725 |
|
|
|
726 |
disable: function () {
|
|
|
727 |
this._destroyEventHandlers();
|
|
|
728 |
$.Widget.prototype.disable.call(this);
|
|
|
729 |
},
|
|
|
730 |
|
|
|
731 |
// This method is exposed to the widget API and allows adding files
|
|
|
732 |
// using the fileupload API. The data parameter accepts an object which
|
|
|
733 |
// must have a files property and can contain additional options:
|
|
|
734 |
// .fileupload('add', {files: filesList});
|
|
|
735 |
add: function (data) {
|
|
|
736 |
if (!data || this.options.disabled) {
|
|
|
737 |
return;
|
|
|
738 |
}
|
|
|
739 |
data.files = $.each($.makeArray(data.files), this._normalizeFile);
|
|
|
740 |
this._onAdd(null, data);
|
|
|
741 |
},
|
|
|
742 |
|
|
|
743 |
// This method is exposed to the widget API and allows sending files
|
|
|
744 |
// using the fileupload API. The data parameter accepts an object which
|
|
|
745 |
// must have a files property and can contain additional options:
|
|
|
746 |
// .fileupload('send', {files: filesList});
|
|
|
747 |
// The method returns a Promise object for the file upload call.
|
|
|
748 |
send: function (data) {
|
|
|
749 |
if (data && !this.options.disabled) {
|
|
|
750 |
data.files = $.each($.makeArray(data.files), this._normalizeFile);
|
|
|
751 |
if (data.files.length) {
|
|
|
752 |
return this._onSend(null, data);
|
|
|
753 |
}
|
|
|
754 |
}
|
|
|
755 |
return this._getXHRPromise(false, data && data.context);
|
|
|
756 |
}
|
|
|
757 |
|
|
|
758 |
});
|
|
|
759 |
|
|
|
760 |
}(jQuery));
|