forked from maccman/holla
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.upload.js
More file actions
73 lines (56 loc) · 1.77 KB
/
Copy pathjquery.upload.js
File metadata and controls
73 lines (56 loc) · 1.77 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
(function($){
$.ajaxTransport("+*", function(s){
var xhr;
if (s.useXHR2)
return {
send: function(headers, complete){
xhr = s.xhr();
xhr.open( s.type, s.url, s.async );
// This needs to be dynamically for the correct multipart boundary
delete headers["Content-Type"];
headers[ "X-Requested-With" ] = "XMLHttpRequest";
for ( i in headers ) {
xhr.setRequestHeader( i, headers[ i ] );
}
var callback = function(e){
var responses = {xml: xhr.responseXML, text: xhr.responseText};
complete( xhr.status, xhr.statusText, responses, xhr.getAllResponseHeaders() );
};
xhr.addEventListener("load", callback);
xhr.addEventListener("error", callback);
if (s.progress)
xhr.addEventListener("progress", s.progress);
if (s.upload && s.upload.success)
xhr.upload.addEventListener("load", s.upload.load);
if (s.upload && s.upload.progress)
xhr.upload.addEventListener("progress", s.upload.progress);
xhr.send(s.data);
},
abort: function(){
if(xhr) xhr.abort();
}
};
});
var defaults = {
processData: false,
contentType: "multipart/form-data",
type: "POST",
useXHR2: true,
upload: {}
};
$.upload = function(url, data, settings){
var fd = new FormData;
if ( data instanceof File )
data = {"file": data}
for ( var key in data )
fd.append(key, data[key]);
// Last argument can be success callback
if ( typeof settings == "function" ) {
settings = {success: settings};
}
settings.url = url;
settings.data = fd;
settings = $.extend({}, defaults, settings);
return $.ajax(settings);
};
})(jQuery);