Created
September 17, 2018 22:46
-
-
Save Elm0D/4d2869189f0b5bd853bc22af65d6ced6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* jquery.binarytransport.js | |
* | |
* @description. jQuery ajax transport for making binary data type requests. | |
* @version 1.0 | |
* @author Henry Algus <[email protected]> | |
* | |
*/ | |
// use this transport for "binary" data type | |
$.ajaxTransport("+binary", function(options, originalOptions, jqXHR){ | |
// check for conditions and support for blob / arraybuffer response type | |
if (window.FormData && ((options.dataType && (options.dataType == 'binary')) || (options.data && ((window.ArrayBuffer && options.data instanceof ArrayBuffer) || (window.Blob && options.data instanceof Blob))))) | |
{ | |
return { | |
// create new XMLHttpRequest | |
send: function(_, callback){ | |
// setup all variables | |
var xhr = new XMLHttpRequest(), | |
url = options.url, | |
type = options.type, | |
// blob or arraybuffer. Default is blob | |
dataType = options.responseType || "blob", | |
data = options.data || null; | |
xhr.addEventListener('load', function(){ | |
var data = {}; | |
data[options.dataType] = xhr.response; | |
// make callback and send data | |
callback(xhr.status, xhr.statusText, data, xhr.getAllResponseHeaders()); | |
}); | |
xhr.open(type, url, true); | |
xhr.responseType = dataType; | |
xhr.send(data); | |
}, | |
abort: function(){ | |
jqXHR.abort(); | |
} | |
}; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment