-
Notifications
You must be signed in to change notification settings - Fork 215
/
jquery.ajaxfileupload.js
116 lines (98 loc) · 3.34 KB
/
jquery.ajaxfileupload.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
/**
* AJAX File Upload
* http://github.com/davgothic/AjaxFileUpload
*
* Copyright (c) 2010-2013 David Hancock (http://davidhancock.co)
*
* Thanks to Steven Barnett for his generous contributions
*
* Licensed under the MIT license ( http://www.opensource.org/licenses/MIT )
*/
;(function ($) {
$.fn.AjaxFileUpload = function (options) {
var defaults = {
action: 'upload.php',
onChange: function (filename) {},
onSubmit: function (filename) {},
onComplete: function (filename, response) {}
},
settings = $.extend({}, defaults, options),
randomId = (function () {
var id = 0;
return function () {
return '_AjaxFileUpload' + id++;
};
})();
return this.each(function () {
var $this = $(this);
if ($this.is('input') && $this.attr('type') === 'file') {
$this.bind('change', onChange);
}
});
function onChange (e) {
var $element = $(e.target),
id = $element.attr('id'),
$clone = $element.removeAttr('id').clone().attr('id', id).AjaxFileUpload(options),
filename = $element.val().replace(/.*(\/|\\)/, ''),
iframe = createIframe(),
form = createForm(iframe);
// We append a clone since the original input will be destroyed
$clone.insertBefore($element);
settings.onChange.call($clone[0], filename);
iframe.bind('load', {element: $clone, form: form, filename: filename}, onComplete);
form.append($element).bind('submit', {element: $clone, iframe: iframe, filename: filename}, onSubmit).submit();
}
function onSubmit (e) {
var data = settings.onSubmit.call(e.data.element, e.data.filename);
// If false cancel the submission
if (data === false) {
// Remove the temporary form and iframe
$(e.target).remove();
e.data.iframe.remove();
return false;
} else {
// Else, append additional inputs
for (var variable in data) {
$('<input />')
.attr('type', 'hidden')
.attr('name', variable)
.val(data[variable])
.appendTo(e.target);
}
}
}
function onComplete (e) {
var $iframe = $(e.target),
doc = ($iframe[0].contentWindow || $iframe[0].contentDocument).document,
response = doc.body.innerHTML;
if (response) {
response = $.parseJSON(response);
} else {
response = {};
}
settings.onComplete.call(e.data.element, e.data.filename, response);
// Remove the temporary form and iframe
e.data.form.remove();
$iframe.remove();
}
function createIframe () {
var id = randomId();
// The iframe must be appended as a string otherwise IE7 will pop up the response in a new window
// http://stackoverflow.com/a/6222471/268669
$('body')
.append('<iframe src="javascript:false;" name="' + id + '" id="' + id + '" style="display: none;"></iframe>');
return $('#' + id);
}
function createForm (iframe) {
return $('<form />')
.attr({
method: 'post',
action: settings.action,
enctype: 'multipart/form-data',
target: iframe[0].name
})
.hide()
.appendTo('body');
}
};
})(jQuery);