-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.hiddenUpload.js
140 lines (122 loc) · 3.88 KB
/
jquery.hiddenUpload.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
/**
* jQuery hidden Upload
* Gary Haran (c) 2009 - [email protected]
* Licensed under the MIT license
* some parts from http://valums.com/ajax-upload/ (c) 2008 Andris Valums, http://valums.com
*/
(function($){
function getIframeId(anchor) {
var i = 0;
do {
var id = anchor.attr('id') + "_iframe_" + i++;
} while( window.frames[id] );
return id;
}
function createIframe(anchor, id, options) {
var iframe = $('<iframe src="javascript:false;" />')
.attr('name', id)
.attr('id', id)
.css('display', 'none')
.appendTo(document.body);
iframe.load(function() {
var response;
var d = iframe.contentDocument || window.frames[iframe.attr('name')].document;
if (d.readyState && d.readyState != 'complete') return; // fix opera 9.26
if (d.body && d.body.innerHTML == "false") return ; // fixing Opera 9.64
if (d.body) {
response = d.body.innerHTML;
if (options.responseType && options.responseType.toLowerCase() == 'json') {
if (d.body.firstChild && d.body.firstChild.nodeName.toUpperCase() == 'PRE') {
response = d.body.firstChild.firstChild.nodeValue;
}
response = (response ? eval("(" + response + ")") : {});
}
} else if (d.XMLDocument) {
response = d.XMLDocument;
} else {
response = d;
}
anchor.hoverDiv.remove();
anchor.removeClass(options.hoverClass)
options.onComplete(response);
iframe.src = "javascript:'<html></html>';";
});
return iframe;
}
function createForm(anchor, id, options) {
return $('<form method="post" enctype="multipart/form-data" />')
.attr('action', options.action)
.attr('target', id)
.attr('id', 'form_' + id)
.css({
'position': 'absolute',
'left': '-14480px',
'top': '-14480px'
})
.append(anchor.input)
.appendTo(document.body);
}
// create a form that will upload
function fireUpload(anchor, options){
if (anchor.value === '') {
return
}
var id = getIframeId(anchor);
var iframe = createIframe(anchor, id, options);
var form = createForm(anchor, id, options);
if (options.closeConnection && $.browser.safari){
$.get(options.closeConnection, function() {
form.submit();
});
} else {
form.submit()
}
}
$.fn.ajaxUpload = function(options) {
var options = $.extend({
action: $(this).attr('href'),
name: 'upload',
data: {},
responseType: 'json',
closeConnection: '',
hoverClass: 'hover',
onSubmit: function(){},
onComplete: function(response){}
}, options);
var anchor = this;
// make a div hover above item with file upload in it
$(this).mouseover(function() {
var hoverDivId = $(this).attr('id') + '_upload_iframe';
if (!$('#' + hoverDivId).length){
var hoverDiv = $('<div/>')
.css({
'overflow': 'hidden',
'opacity': '0',
'z-index': 2147483583
})
.attr('id', hoverDivId)
.appendTo($(document.body));
anchor.hoverDiv = hoverDiv;
anchor.input = $('<input type="file" name="' + options.name + '" />').css({
'margin': '-5px 0 0 -175px',
'padding': 0,
'width': '220px',
'height': '30px',
'font-size': '280px',
'cursor': 'pointer'
}).mouseover(function() {
$(anchor).addClass(options.hoverClass);
}).mouseout(function() {
$(anchor).removeClass(options.hoverClass);
}).change(function() {
if (options.onSubmit() !== false){
fireUpload(anchor, options);
}
})
hoverDiv.html(anchor.input);
}
$('#' + hoverDivId).clonePosition(this);
});
return this;
}
})(jQuery);