forked from cloudbase-io/CBHelper-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCBXMLHttpRequest.js
More file actions
153 lines (137 loc) · 4.71 KB
/
Copy pathCBXMLHttpRequest.js
File metadata and controls
153 lines (137 loc) · 4.71 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
142
143
144
145
146
147
148
149
150
151
152
153
/*
Copyright (C) 2012 Cloudbase.io
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License,
version 2, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
/**
* This object is an abstraction of the standard XMLHttpRequest. It uses jquery if
* available otherwise will revert back to the standard broser object. This is only
* used by the helper class internally
*
* @param cbFunction The cloudbase.io function name (data/cloudfunction/applet/downloads etc)
* @param url The url we are calling to
* @param whenDone A callback to be executed once the request is completed
* @returns a new CBXMLHttpRequest object
*/
function CBXMLHttpRequest (cbFunction, url, whenDone) {
/**
* The url to POST the data to
*/
this.requestUrl = url;
/**
* The name of the cloudbase function
*/
this.cloudbaseFunction = cbFunction;
/**
* The callback function when the request is completed
*/
this.requestCompleted = whenDone;
/**
* additional custom headers for the request
*/
this.headers = {};
/**
* A CBPlatformHelper. in this class this is used just for logging
*/
this.platformHelper = null;
/**
* A callback function to monitor the download of a file
* This receives two parameters: The number of bytes downloaded
* so far and the total number of bytes expected
* function(bytesDownloaded, totalBytes)
*/
this.downloadProgress = null;
}
/**
* Adds a new custom header to the request
* @param key The name for the header
* @param value The value for the new custom header
*/
CBXMLHttpRequest.prototype.setRequestHeader = function(key, value) {
this.headers[key] = value;
};
CBXMLHttpRequest.getBlobBuilder = function() {
if (window.MSBlobBuilder)
return new MSBlobBuilder();
if (window.MozBlobBuilder)
return new MozBlobBuilder();
if (window.WebKitBloblBuilder)
return new WebKitBlobBuilder();
if (window.BlobBuilder)
return new BlobBuilder();
return null;
}
/**
* Sends the request and calls the callback functions during a downloads
* and once the request is completed
* @param formData A value string representing the multipart/form-data object or a browser FormData object
* @param aSync whether the request should be asynchronous
*/
CBXMLHttpRequest.prototype.send = function(formData, aSync) {
var oRequest = this;
if (this.cloudbaseFunction == "download") {
var xhr = new XMLHttpRequest();
xhr.open("POST", this.requestUrl, true);
// Set the responseType to arraybuffer. "blob" is an option too, rendering BlobBuilder unnecessary, but the support for "blob" is not widespread enough yet
xhr.responseType = "arraybuffer";
//xhr.overrideMimeType("text/plain; charset=x-user-defined");
//xhr.overrideMimeType('text\/plain; charset=x-user-defined');
xhr.addEventListener("load", function () {
if (xhr.status == 200) {
//oRequest.log("resp: " + xhr.response)
// Create a blob from the response
//var blob = new Blob([xhr.response], {type: "image/png" });
var blob = new Blob([xhr.response], { type: xhr.getResponseHeader('Content-type') });
if (oRequest.requestCompleted !== "undefined") {
oRequest.requestCompleted(xhr.status, blob);
}
}
}, false);
xhr.addEventListener("progress", function(evt) {
if (evt.lengthComputable) {
//evt.loaded the bytes browser receive
//evt.total the total bytes seted by the header
oRequest.downloadProgress(evt.loaded, evt.total);
}
});
// Send XHR
xhr.send(formData);
} else {
if (typeof jQuery !== 'undefined') {
$.ajax({
type: "POST",
url: oRequest.requestUrl,
data: formData,
async: aSync,
processData: false,
contentType: false,
crossDomain: true,
headers: {
x_cb_js_class : "true"
},
complete: function(httpResponse, textStatus) {
if (oRequest.requestCompleted !== "undefined") {
oRequest.requestCompleted(httpResponse.status, httpResponse.responseText);
}
}
});
} else {
var xhr = new XMLHttpRequest();
xhr.open("POST", this.requestUrl, true);
xhr.addEventListener("load", function () {
if (oRequest.requestCompleted)
oRequest.requestCompleted(xhr.status, xhr.responseText);
}, false);
xhr.send(formData);
}
}
};