Guest User

Untitled

a guest
Feb 6th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. var xhr = new XMLHttpRequest();
  2. xhr.open('GET', 'blob:http%3A//your.blob.url.here', true);
  3. xhr.responseType = 'blob';
  4. xhr.onload = function(e) {
  5. if (this.status == 200) {
  6. var myBlob = this.response;
  7. // myBlob is now the blob that the object URL pointed to.
  8. }
  9. };
  10. xhr.send();
  11.  
  12. xhr.responseType = 'arraybuffer';
  13.  
  14. canvas.toBlob(function(my_file){
  15. //.toBlob is only implemented in > FF18 but there is a polyfill
  16. //for other browsers https://github.com/blueimp/JavaScript-Canvas-to-Blob
  17. var myBlob = (my_file);
  18. })
  19.  
  20. /**
  21. *
  22. * jquery.binarytransport.js
  23. *
  24. * @description. jQuery ajax transport for making binary data type requests.
  25. * @version 1.0
  26. * @author Henry Algus <henryalgus@gmail.com>
  27. *
  28. */
  29.  
  30. // use this transport for "binary" data type
  31. $.ajaxTransport("+binary", function (options, originalOptions, jqXHR) {
  32. // check for conditions and support for blob / arraybuffer response type
  33. if (window.FormData && ((options.dataType && (options.dataType == 'binary')) || (options.data && ((window.ArrayBuffer && options.data instanceof ArrayBuffer) || (window.Blob && options.data instanceof Blob))))) {
  34. return {
  35. // create new XMLHttpRequest
  36. send: function (headers, callback) {
  37. // setup all variables
  38. var xhr = new XMLHttpRequest(),
  39. url = options.url,
  40. type = options.type,
  41. async = options.async || true,
  42. // blob or arraybuffer. Default is blob
  43. dataType = options.responseType || "blob",
  44. data = options.data || null,
  45. username = options.username || null,
  46. password = options.password || null;
  47.  
  48. xhr.addEventListener('load', function () {
  49. var data = {};
  50. data[options.dataType] = xhr.response;
  51. // make callback and send data
  52. callback(xhr.status, xhr.statusText, data, xhr.getAllResponseHeaders());
  53. });
  54.  
  55. xhr.open(type, url, async, username, password);
  56.  
  57. // setup custom headers
  58. for (var i in headers) {
  59. xhr.setRequestHeader(i, headers[i]);
  60. }
  61.  
  62. xhr.responseType = dataType;
  63. xhr.send(data);
  64. },
  65. abort: function () {
  66. jqXHR.abort();
  67. }
  68. };
  69. }
  70. });
  71.  
  72. function downloadArt(url)
  73. {
  74. $.ajax(url, {
  75. dataType: "binary",
  76. processData: false
  77. }).done(function (data) {
  78. // just my logic to name/create files
  79. var filename = url.substr(url.lastIndexOf('/') + 1) + '.png';
  80. var blob = new Blob([data], { type: 'image/png' });
  81.  
  82. saveAs(blob, filename);
  83. });
  84. }
  85.  
  86. axios({
  87. method: 'get',
  88. url: file[0].preview, // blob url eg. blob:http://127.0.0.1:8000/e89c5d87-a634-4540-974c-30dc476825cc
  89. responseType: 'blob'
  90. }).then(function(response){
  91. var reader = new FileReader();
  92. reader.readAsDataURL(response.data);
  93. reader.onloadend = function() {
  94. var base64data = reader.result;
  95. self.props.onMainImageDrop(base64data)
  96. }
  97.  
  98. })
Add Comment
Please, Sign In to add comment