forked from bassjobsen/jqueryupload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjqueryupload.js
More file actions
141 lines (120 loc) · 3.19 KB
/
Copy pathjqueryupload.js
File metadata and controls
141 lines (120 loc) · 3.19 KB
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
/*
* jQuery.upload v1.0.3
*
* Copyright (c) 2013 Bass Jobsen
* http://www.w3masters.nl/
* Dual licensed under the MIT and GPL licenses.
*
* Original written by Lagos (http://lagoscript.org/)
*
* Contributors:
* Mr Rogers http://rcode5.wordpress.com/
*/
(function($) {
var uuid = 0;
$.fn.upload = function(url, data, callback, type) {
var self = this, inputs, checkbox, checked,
iframeName = 'jquery_upload' + ++uuid,
iframe = $('<iframe name="' + iframeName + '" style="position:absolute;top:-9999px" />').appendTo('body'),
form = '<form target="' + iframeName + '" method="post" enctype="multipart/form-data" />';
if ($.isFunction(data)) {
type = callback;
callback = data;
data = {};
}
checkbox = $('input:checkbox', this);
checked = $('input:checked', this);
form = self.wrapAll(form).parent('form').attr('action', url);
// Make sure radios and checkboxes keep original values
// (IE resets checkd attributes when appending)
checkbox.removeAttr('checked');
checked.attr('checked', true);
inputs = createInputs(data);
inputs = inputs ? $(inputs).appendTo(form) : null;
form.submit(function() {
iframe.load(function() {
var data = handleData(this, type),
checked = $('input:checked', self);
form.after(self).remove();
checkbox.removeAttr('checked');
checked.attr('checked', true);
if (inputs) {
inputs.remove();
}
setTimeout(function() {
iframe.remove();
if (type === 'script') {
$.globalEval(data);
}
if (callback) {
callback.call(self, data);
}
}, 0);
});
}).submit();
return this;
};
function createInputs(data) {
return $.map(param(data), function(param) {
var e = $(document.createElement('input'));
e.attr('type', 'hidden');
e.attr('name', param.name);
e.attr('value', param.value);
return e;
});
}
function param(data) {
if ($.isArray(data)) {
return data;
}
var params = [];
function add(name, value) {
params.push({name:name, value:value});
}
if (typeof data === 'object') {
$.each(data, function(name) {
if ($.isArray(this)) {
$.each(this, function() {
add(name, this);
});
} else {
add(name, $.isFunction(this) ? this() : this);
}
});
} else if (typeof data === 'string') {
$.each(data.split('&'), function() {
var param = $.map(this.split('='), function(v) {
return decodeURIComponent(v.replace(/\+/g, ' '));
});
add(param[0], param[1]);
});
}
return params;
}
function handleData(iframe, type) {
var data, contents = $(iframe).contents().get(0);
if ($.isXMLDoc(contents) || contents.XMLDocument) {
return contents.XMLDocument || contents;
}
data = $(contents).find('body').text();
switch (type) {
case 'xml':
data = parseXml(data);
break;
case 'json':
data = $.parseJSON(data);
break;
}
return data;
}
function parseXml(text) {
if (window.DOMParser) {
return new DOMParser().parseFromString(text, 'application/xml');
} else {
var xml = new ActiveXObject('Microsoft.XMLDOM');
xml.async = false;
xml.loadXML(text);
return xml;
}
}
})(jQuery);