forked from blueimp/jQuery-File-Upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.fileupload.js
431 lines (400 loc) · 16.3 KB
/
jquery.fileupload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
* jQuery File Upload Plugin 1.2
*
* Copyright 2010, Sebastian Tschan, AQUANTUM
* Licensed under the MIT license:
* http://creativecommons.org/licenses/MIT/
*
* https://blueimp.net
* http://www.aquantum.de
*/
/*jslint browser: true */
/*global File, FileReader, FormData, unescape, jQuery */
(function ($) {
var FileUpload = function (dropZone) {
var fileUpload = this,
uploadForm = (dropZone.is('form') ? dropZone : dropZone.find('form')),
fileInput = dropZone.find('input:file'),
settings = {
namespace: 'file_upload',
cssClass: 'file_upload',
url: uploadForm.attr('action'),
method: uploadForm.attr('method'),
fieldName: fileInput.attr('name'),
streaming: false,
formData: function () {
return uploadForm.serializeArray();
},
withCredentials: false
},
documentListeners = {},
dropZoneListeners = {},
fileInputListeners = {},
undef = 'undefined',
protocolRegExp = /^http(s)?:\/\//,
iframe,
inputChangeCalled,
isXHRUploadCapable = function () {
return typeof XMLHttpRequest !== undef && typeof File !== undef && (
settings.streaming || typeof FormData !== undef || typeof FileReader !== undef
);
},
initEventHandlers = function () {
documentListeners['dragenter.' + settings.namespace] = function (e) {
fileUpload.documentDragEnter(e);
};
documentListeners['dragover.' + settings.namespace] = function (e) {
fileUpload.documentDragOver(e);
};
documentListeners['dragleave.' + settings.namespace] = function (e) {
fileUpload.documentDragLeave(e);
};
$(document).bind(documentListeners);
dropZoneListeners['dragenter.' + settings.namespace] = function (e) {
fileUpload.dragEnter(e);
};
dropZoneListeners['dragover.' + settings.namespace] = function (e) {
fileUpload.dragOver(e);
};
dropZoneListeners['dragleave.' + settings.namespace] = function (e) {
fileUpload.dragLeave(e);
};
dropZoneListeners['drop.' + settings.namespace] = function (e) {
fileUpload.drop(e);
};
dropZone.bind(dropZoneListeners);
fileInputListeners['click.' + settings.namespace] = function (e) {
fileUpload.inputClick(e);
};
fileInputListeners['change.' + settings.namespace] = function (e) {
fileUpload.inputChange(e);
};
fileInput.bind(fileInputListeners);
},
removeEventHandlers = function () {
$.each(documentListeners, function (i, listener) {
if (documentListeners.hasOwnProperty(listener)) {
$(document).unbind(listener);
}
});
$.each(dropZoneListeners, function (i, listener) {
if (dropZoneListeners.hasOwnProperty(listener)) {
dropZone.unbind(listener);
}
});
$.each(fileInputListeners, function (i, listener) {
if (fileInputListeners.hasOwnProperty(listener)) {
fileInput.unbind(listener);
}
});
},
initUploadEventHandlers = function (files, index, xhr, settings) {
if (typeof settings.progress === 'function') {
xhr.upload.onprogress = function (e) {
settings.progress(e, files, index, xhr, settings);
};
}
if (typeof settings.load === 'function') {
xhr.onload = function (e) {
settings.load(e, files, index, xhr, settings);
};
}
if (typeof settings.abort === 'function') {
xhr.onabort = function (e) {
settings.abort(e, files, index, xhr, settings);
};
}
if (typeof settings.error === 'function') {
xhr.onerror = function (e) {
settings.error(e, files, index, xhr, settings);
};
}
},
getFormData = function (settings) {
if (typeof settings.formData === 'function') {
return settings.formData();
} else if ($.isArray(settings.formData)) {
return settings.formData;
} else if (settings.formData) {
var formData = [];
$.each(settings.formData, function (name, value) {
formData.push({name: name, value: value});
});
return formData;
}
return [];
},
buildMultiPartFormData = function (boundary, file, fileContent, fields) {
var doubleDash = '--',
crlf = '\r\n',
formData = '';
$.each(fields, function (index, field) {
formData += doubleDash + boundary + crlf +
'Content-Disposition: form-data; name="' +
unescape(encodeURIComponent(field.name)) +
'"' + crlf + crlf +
unescape(encodeURIComponent(field.value)) + crlf;
});
formData += doubleDash + boundary + crlf +
'Content-Disposition: form-data; name="' +
unescape(encodeURIComponent(settings.fieldName)) +
'"; filename="' + unescape(encodeURIComponent(file.name)) + '"' + crlf +
'Content-Type: ' + file.type + crlf + crlf +
fileContent + crlf +
doubleDash + boundary + doubleDash + crlf;
return formData;
},
isSameDomain = function (url) {
if (protocolRegExp.test(url)) {
var host = location.host,
indexStart = location.protocol.length + 2,
index = url.indexOf(host, indexStart),
pathIndex = index + host.length;
if ((index === indexStart || index === url.indexOf('@', indexStart) + 1) && (
url.length === pathIndex || $.inArray(url.charAt(pathIndex), ['/', '?', '#']))) {
return true;
}
return false;
}
return true;
},
uploadFile = function (files, index, xhr, settings) {
var sameDomain = isSameDomain(settings.url),
file = files[index],
formData, fileReader;
initUploadEventHandlers(files, index, xhr, settings);
xhr.open(settings.method, settings.url, true);
if (sameDomain) {
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
} else if (settings.withCredentials) {
xhr.withCredentials = true;
}
if (settings.streaming) {
if (sameDomain) {
xhr.setRequestHeader('X-File-Name', unescape(encodeURIComponent(file.name)));
}
xhr.setRequestHeader('Content-Type', file.type);
xhr.send(file);
} else {
if (typeof FormData !== undef) {
formData = new FormData();
$.each(getFormData(settings), function (index, field) {
formData.append(field.name, field.value);
});
formData.append(settings.fieldName, file);
xhr.send(formData);
} else if (typeof FileReader !== undef) {
fileReader = new FileReader();
fileReader.onload = function (e) {
var fileContent = e.target.result,
boundary = '----MultiPartFormBoundary' + (new Date()).getTime();
xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
xhr.sendAsBinary(buildMultiPartFormData(
boundary,
file,
fileContent,
getFormData(settings)
));
};
fileReader.readAsBinaryString(file);
} else {
$.error('Browser does neither support FormData nor FileReader interface');
}
}
},
handleFile = function (files, index) {
var xhr = new XMLHttpRequest();
if (typeof settings.init === 'function') {
settings.init(files, index, xhr, function (options) {
uploadFile(files, index, xhr, $.extend({}, settings, options));
});
} else {
uploadFile(files, index, xhr, settings);
}
},
handleFiles = function (files) {
for (var i = 0; i < files.length; i += 1) {
handleFile(files, i);
}
},
legacyUpload = function (input, settings) {
uploadForm.attr('action', settings.url)
.attr('target', iframe.attr('name'));
if (typeof settings.load === 'function') {
iframe.unbind('load.' + settings.namespace)
.bind('load.' + settings.namespace, function (e) {
settings.load(e, [{name: input.value, type: null, size: null}], 0, null, settings);
});
}
var formData = getFormData(settings);
uploadForm.find(':input[type!=file]').not(':disabled')
.attr('disabled', true).addClass(settings.namespace + '_disabled');
$.each(formData, function (index, field) {
uploadForm.append(
$('<input type="hidden"/>')
.attr('name', field.name).val(field.value)
.addClass(settings.namespace + '_form_data')
);
});
uploadForm.get(0).submit();
uploadForm.find('.' + settings.namespace + '_disabled')
.removeAttr('disabled').removeClass(settings.namespace + '_disabled');
uploadForm.find('.' + settings.namespace + '_form_data').remove();
},
handleLegacyUpload = function (input) {
if (typeof settings.init === 'function') {
settings.init([{name: input.value, type: null, size: null}], 0, null, function (options) {
legacyUpload(input, $.extend({}, settings, options));
});
} else {
legacyUpload(input, settings);
}
};
this.documentDragEnter = function (e) {
if (typeof settings.documentDragEnter === 'function') {
if (settings.documentDragEnter(e) === false) {
return;
}
}
e.preventDefault();
};
this.documentDragOver = function (e) {
if (typeof settings.documentDragOver === 'function') {
if (settings.documentDragOver(e) === false) {
return;
}
}
var dataTransfer = e.originalEvent.dataTransfer,
target = e.originalEvent.target;
if (dataTransfer && dropZone.get(0) !== target && !dropZone.has(target).length) {
dataTransfer.dropEffect = 'none';
}
e.preventDefault();
};
this.documentDragLeave = function (e) {
if (typeof settings.documentDragLeave === 'function') {
if (settings.documentDragLeave(e) === false) {
return;
}
}
e.preventDefault();
};
this.dragEnter = function (e) {
if (typeof settings.dragEnter === 'function') {
if (settings.dragEnter(e) === false) {
return;
}
}
e.preventDefault();
};
this.dragOver = function (e) {
if (typeof settings.dragOver === 'function') {
if (settings.dragOver(e) === false) {
return;
}
}
var dataTransfer = e.originalEvent.dataTransfer;
if (dataTransfer) {
dataTransfer.dropEffect = dataTransfer.effectAllowed = 'copy';
}
e.preventDefault();
e.stopPropagation();
};
this.dragLeave = function (e) {
if (typeof settings.dragLeave === 'function') {
if (settings.dragLeave(e) === false) {
return;
}
}
e.preventDefault();
};
this.drop = function (e) {
if (typeof settings.drop === 'function') {
if (settings.drop(e) === false) {
return;
}
}
var dataTransfer = e.originalEvent.dataTransfer;
if (dataTransfer && dataTransfer.files && isXHRUploadCapable()) {
handleFiles(dataTransfer.files);
}
e.preventDefault();
};
this.inputClick = function (e) {
if (typeof settings.inputClick === 'function') {
if (settings.inputClick(e) === false) {
return;
}
}
e.target.form.reset();
inputChangeCalled = false;
};
this.inputChange = function (e) {
if (inputChangeCalled) {
return;
}
inputChangeCalled = true;
if (typeof settings.inputChange === 'function') {
if (settings.inputChange(e) === false) {
return;
}
}
if (e.target.files && isXHRUploadCapable()) {
handleFiles(e.target.files);
} else {
handleLegacyUpload(e.target);
}
};
this.init = function (options) {
if (options) {
$.extend(settings, options);
}
if (dropZone.data(settings.namespace)) {
$.error('FileUpload with namespace "' + settings.namespace + '" already assigned to this element');
return;
}
dropZone.data(settings.namespace, fileUpload)
.addClass(settings.cssClass);
if (!isXHRUploadCapable()) {
// javascript:false as iframe src prevents warning popups on HTTPS in IE6:
iframe = $('<iframe src="javascript:false;" name="iframe_' + settings.namespace + '"></iframe>')
.appendTo(dropZone);
}
initEventHandlers();
};
this.destroy = function () {
removeEventHandlers();
dropZone.removeData(settings.namespace)
.removeClass(settings.cssClass)
.find('iframe[name=iframe_' + settings.namespace + ']')
.unbind('load.' + settings.namespace).remove();
};
},
methods = {
init : function (options) {
return this.each(function () {
(new FileUpload($(this))).init(options);
});
},
destroy : function (namespace) {
return this.each(function () {
namespace = namespace ? namespace : 'fileUpload';
var fileUpload = $(this).data(namespace);
if (fileUpload) {
fileUpload.destroy();
} else {
$.error('No FileUpload with namespace "' + namespace + '" assigned to this element');
}
});
}
};
$.fn.fileUpload = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || ! method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.fileUpload');
}
};
}(jQuery));