You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.6 KiB

6 years ago
  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. // use this transport for "binary" data type
  11. $.ajaxTransport("+binary", function(options, originalOptions, jqXHR){
  12. // check for conditions and support for blob / arraybuffer response type
  13. if (window.FormData && ((options.dataType && (options.dataType == 'binary')) || (options.data && ((window.ArrayBuffer && options.data instanceof ArrayBuffer) || (window.Blob && options.data instanceof Blob)))))
  14. {
  15. return {
  16. // create new XMLHttpRequest
  17. send: function(headers, callback){
  18. // setup all variables
  19. var xhr = new XMLHttpRequest(),
  20. url = options.url,
  21. type = options.type,
  22. async = options.async || true,
  23. // blob or arraybuffer. Default is blob
  24. dataType = options.responseType || "blob",
  25. data = options.data || null,
  26. username = options.username || null,
  27. password = options.password || null;
  28. xhr.addEventListener('load', function(){
  29. var data = {};
  30. data[options.dataType] = xhr.response;
  31. // make callback and send data
  32. callback(xhr.status, xhr.statusText, data, xhr.getAllResponseHeaders());
  33. });
  34. xhr.open(type, url, async, username, password);
  35. // setup custom headers
  36. for (var i in headers ) {
  37. xhr.setRequestHeader(i, headers[i] );
  38. }
  39. xhr.responseType = dataType;
  40. xhr.send(data);
  41. },
  42. abort: function(){
  43. jqXHR.abort();
  44. }
  45. };
  46. }
  47. });