| 9 |
lars |
1 |
/*
|
|
|
2 |
* jQuery Iframe Transport Plugin 1.2.2
|
|
|
3 |
* https://github.com/blueimp/jQuery-File-Upload
|
|
|
4 |
*
|
|
|
5 |
* Copyright 2011, Sebastian Tschan
|
|
|
6 |
* https://blueimp.net
|
|
|
7 |
*
|
|
|
8 |
* Licensed under the MIT license:
|
|
|
9 |
* http://creativecommons.org/licenses/MIT/
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
/*jslint unparam: true */
|
|
|
13 |
/*global jQuery */
|
|
|
14 |
|
|
|
15 |
(function ($) {
|
|
|
16 |
'use strict';
|
|
|
17 |
|
|
|
18 |
// Helper variable to create unique names for the transport iframes:
|
|
|
19 |
var counter = 0;
|
|
|
20 |
|
|
|
21 |
// The iframe transport accepts three additional options:
|
|
|
22 |
// options.fileInput: a jQuery collection of file input fields
|
|
|
23 |
// options.paramName: the parameter name for the file form data,
|
|
|
24 |
// overrides the name property of the file input field(s)
|
|
|
25 |
// options.formData: an array of objects with name and value properties,
|
|
|
26 |
// equivalent to the return data of .serializeArray(), e.g.:
|
|
|
27 |
// [{name: a, value: 1}, {name: b, value: 2}]
|
|
|
28 |
$.ajaxTransport('iframe', function (options, originalOptions, jqXHR) {
|
|
|
29 |
if (options.type === 'POST' || options.type === 'GET') {
|
|
|
30 |
var form,
|
|
|
31 |
iframe;
|
|
|
32 |
return {
|
|
|
33 |
send: function (headers, completeCallback) {
|
|
|
34 |
form = $('<form style="display:none;"></form>');
|
|
|
35 |
// javascript:false as initial iframe src
|
|
|
36 |
// prevents warning popups on HTTPS in IE6.
|
|
|
37 |
// IE versions below IE8 cannot set the name property of
|
|
|
38 |
// elements that have already been added to the DOM,
|
|
|
39 |
// so we set the name along with the iframe HTML markup:
|
|
|
40 |
iframe = $(
|
|
|
41 |
'<iframe src="javascript:false;" name="iframe-transport-' +
|
|
|
42 |
(counter += 1) + '"></iframe>'
|
|
|
43 |
).bind('load', function () {
|
|
|
44 |
var fileInputClones;
|
|
|
45 |
iframe
|
|
|
46 |
.unbind('load')
|
|
|
47 |
.bind('load', function () {
|
|
|
48 |
var response;
|
|
|
49 |
// Wrap in a try/catch block to catch exceptions thrown
|
|
|
50 |
// when trying to access cross-domain iframe contents:
|
|
|
51 |
try {
|
|
|
52 |
response = iframe.contents();
|
|
|
53 |
// Google Chrome and Firefox do not throw an
|
|
|
54 |
// exception when calling iframe.contents() on
|
|
|
55 |
// cross-domain requests, so we unify the response:
|
|
|
56 |
if (!response.length || !response[0].firstChild) {
|
|
|
57 |
throw new Error();
|
|
|
58 |
}
|
|
|
59 |
} catch (e) {
|
|
|
60 |
response = undefined;
|
|
|
61 |
}
|
|
|
62 |
// The complete callback returns the
|
|
|
63 |
// iframe content document as response object:
|
|
|
64 |
completeCallback(
|
|
|
65 |
200,
|
|
|
66 |
'success',
|
|
|
67 |
{'iframe': response}
|
|
|
68 |
);
|
|
|
69 |
// Fix for IE endless progress bar activity bug
|
|
|
70 |
// (happens on form submits to iframe targets):
|
|
|
71 |
$('<iframe src="javascript:false;"></iframe>')
|
|
|
72 |
.appendTo(form);
|
|
|
73 |
form.remove();
|
|
|
74 |
});
|
|
|
75 |
form
|
|
|
76 |
.prop('target', iframe.prop('name'))
|
|
|
77 |
.prop('action', options.url)
|
|
|
78 |
.prop('method', options.type);
|
|
|
79 |
if (options.formData) {
|
|
|
80 |
$.each(options.formData, function (index, field) {
|
|
|
81 |
$('<input type="hidden"/>')
|
|
|
82 |
.prop('name', field.name)
|
|
|
83 |
.val(field.value)
|
|
|
84 |
.appendTo(form);
|
|
|
85 |
});
|
|
|
86 |
}
|
|
|
87 |
if (options.fileInput && options.fileInput.length &&
|
|
|
88 |
options.type === 'POST') {
|
|
|
89 |
fileInputClones = options.fileInput.clone();
|
|
|
90 |
// Insert a clone for each file input field:
|
|
|
91 |
options.fileInput.after(function (index) {
|
|
|
92 |
return fileInputClones[index];
|
|
|
93 |
});
|
|
|
94 |
if (options.paramName) {
|
|
|
95 |
options.fileInput.each(function () {
|
|
|
96 |
$(this).prop('name', options.paramName);
|
|
|
97 |
});
|
|
|
98 |
}
|
|
|
99 |
// Appending the file input fields to the hidden form
|
|
|
100 |
// removes them from their original location:
|
|
|
101 |
form
|
|
|
102 |
.append(options.fileInput)
|
|
|
103 |
.prop('enctype', 'multipart/form-data')
|
|
|
104 |
// enctype must be set as encoding for IE:
|
|
|
105 |
.prop('encoding', 'multipart/form-data');
|
|
|
106 |
}
|
|
|
107 |
form.submit();
|
|
|
108 |
// Insert the file input fields at their original location
|
|
|
109 |
// by replacing the clones with the originals:
|
|
|
110 |
if (fileInputClones && fileInputClones.length) {
|
|
|
111 |
options.fileInput.each(function (index, input) {
|
|
|
112 |
var clone = $(fileInputClones[index]);
|
|
|
113 |
$(input).prop('name', clone.prop('name'));
|
|
|
114 |
clone.replaceWith(input);
|
|
|
115 |
});
|
|
|
116 |
}
|
|
|
117 |
});
|
|
|
118 |
form.append(iframe).appendTo('body');
|
|
|
119 |
},
|
|
|
120 |
abort: function () {
|
|
|
121 |
if (iframe) {
|
|
|
122 |
// javascript:false as iframe src aborts the request
|
|
|
123 |
// and prevents warning popups on HTTPS in IE6.
|
|
|
124 |
// concat is used to avoid the "Script URL" JSLint error:
|
|
|
125 |
iframe
|
|
|
126 |
.unbind('load')
|
|
|
127 |
.prop('src', 'javascript'.concat(':false;'));
|
|
|
128 |
}
|
|
|
129 |
if (form) {
|
|
|
130 |
form.remove();
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
};
|
|
|
134 |
}
|
|
|
135 |
});
|
|
|
136 |
|
|
|
137 |
// The iframe transport returns the iframe content document as response.
|
|
|
138 |
// The following adds converters from iframe to text, json, html, and script:
|
|
|
139 |
$.ajaxSetup({
|
|
|
140 |
converters: {
|
|
|
141 |
'iframe text': function (iframe) {
|
|
|
142 |
return iframe.text();
|
|
|
143 |
},
|
|
|
144 |
'iframe json': function (iframe) {
|
|
|
145 |
return $.parseJSON(iframe.text());
|
|
|
146 |
},
|
|
|
147 |
'iframe html': function (iframe) {
|
|
|
148 |
return iframe.find('body').html();
|
|
|
149 |
},
|
|
|
150 |
'iframe script': function (iframe) {
|
|
|
151 |
return $.globalEval(iframe.text());
|
|
|
152 |
}
|
|
|
153 |
}
|
|
|
154 |
});
|
|
|
155 |
|
|
|
156 |
}(jQuery));
|