foist
[kismet-logviewer.git] / logviewer / static / js / jquery.ajax.binary.js
1 /**
2  *
3  * jquery.binarytransport.js
4  *
5  * @description. jQuery ajax transport for making binary data type requests.
6  * @version 1.0 
7  * @author Henry Algus <henryalgus@gmail.com>
8  *
9  */
10  
11 // use this transport for "binary" data type
12 $.ajaxTransport("+binary", function(options, originalOptions, jqXHR){
13     // check for conditions and support for blob / arraybuffer response type
14     if (window.FormData && ((options.dataType && (options.dataType == 'binary')) || (options.data && ((window.ArrayBuffer && options.data instanceof ArrayBuffer) || (window.Blob && options.data instanceof Blob)))))
15     {
16         return {
17             // create new XMLHttpRequest
18             send: function(headers, callback){
19         // setup all variables
20                 var xhr = new XMLHttpRequest(),
21         url = options.url,
22         type = options.type,
23         async = options.async || true,
24         // blob or arraybuffer. Default is blob
25         dataType = options.responseType || "blob",
26         data = options.data || null,
27         username = options.username || null,
28         password = options.password || null;
29                     
30                 xhr.addEventListener('load', function(){
31             var data = {};
32             data[options.dataType] = xhr.response;
33             // make callback and send data
34             callback(xhr.status, xhr.statusText, data, xhr.getAllResponseHeaders());
35                 });
36  
37                 xhr.open(type, url, async, username, password);
38                 
39         // setup custom headers
40         for (var i in headers ) {
41             xhr.setRequestHeader(i, headers[i] );
42         }
43                 
44                 xhr.responseType = dataType;
45                 xhr.send(data);
46             },
47             abort: function(){
48                 jqXHR.abort();
49             }
50         };
51     }
52 });