Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. function base64ArrayBuffer(arrayBuffer) {
  2. var base64 = ''
  3. var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  4.  
  5. var bytes = new Uint8Array(arrayBuffer)
  6. var byteLength = bytes.byteLength
  7. var byteRemainder = byteLength % 3
  8. var mainLength = byteLength - byteRemainder
  9.  
  10. var a, b, c, d
  11. var chunk
  12.  
  13. // Main loop deals with bytes in chunks of 3
  14. for (var i = 0; i < mainLength; i = i + 3) {
  15. // Combine the three bytes into a single integer
  16. chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]
  17.  
  18. // Use bitmasks to extract 6-bit segments from the triplet
  19. a = (chunk & 16515072) >> 18 // 16515072 = (2^6 - 1) << 18
  20. b = (chunk & 258048) >> 12 // 258048 = (2^6 - 1) << 12
  21. c = (chunk & 4032) >> 6 // 4032 = (2^6 - 1) << 6
  22. d = chunk & 63 // 63 = 2^6 - 1
  23.  
  24. // Convert the raw binary segments to the appropriate ASCII encoding
  25. base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d]
  26. }
  27.  
  28. // Deal with the remaining bytes and padding
  29. if (byteRemainder == 1) {
  30. chunk = bytes[mainLength]
  31.  
  32. a = (chunk & 252) >> 2 // 252 = (2^6 - 1) << 2
  33.  
  34. // Set the 4 least significant bits to zero
  35. b = (chunk & 3) << 4 // 3 = 2^2 - 1
  36.  
  37. base64 += encodings[a] + encodings[b] + '=='
  38. } else if (byteRemainder == 2) {
  39. chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1]
  40.  
  41. a = (chunk & 64512) >> 10 // 64512 = (2^6 - 1) << 10
  42. b = (chunk & 1008) >> 4 // 1008 = (2^6 - 1) << 4
  43.  
  44. // Set the 2 least significant bits to zero
  45. c = (chunk & 15) << 2 // 15 = 2^4 - 1
  46.  
  47. base64 += encodings[a] + encodings[b] + encodings[c] + '='
  48. }
  49.  
  50. return base64
  51. }
  52.  
  53. $('.upload_btn').on("click", function (e) {
  54. e.preventDefault();
  55.  
  56. if ($("#upload_control")[0].files.length === 0) {
  57. $.fancybox.open("<div class='nofileselected'><h2>No File Selected</h2><p>No file has been selected to upload.</p></div>");
  58. } else {
  59. var Filename = $("#upload_control")[0].files[0].name;
  60. var Maxuploadsize = "<%=ImageMaximumUploadSize%>";
  61. var Sizeoffile = $("#upload_control")[0].files[0].size;
  62.  
  63.  
  64. var fileList = document.getElementById("upload_control").files;
  65. var fileReader = new FileReader();
  66. if (fileReader && fileList && fileList.length) {
  67. fileReader.readAsArrayBuffer(fileList[0]);
  68. fileReader.onload = function () {
  69. var imageData = fileReader.result;
  70. var NewData = base64ArrayBuffer(imageData)
  71. uploadImageAjax(NewData);
  72. };
  73. }
  74.  
  75.  
  76. function uploadImageAjax(NewData){
  77. $.ajax({
  78. type: "POST",
  79. cache: false,
  80. processData: false,
  81. contentType: false,
  82. async: true,
  83. contentType: 'application/json',
  84. url: $.fn.GetBaseURL() + 'DesktopModules/ProductDetailedView/API/Upload/UploadImage?FileName=' + Filename + '&MaxUploadSize=' + Maxuploadsize + '&SizeOfFile=' + Sizeoffile + '&DataFile=' + NewData,
  85. success: function (data) {
  86. alert("Upload Successfull");
  87. },
  88. error: function (xhr, ajaxOptions, thrownError) {
  89. console.log(xhr.responseText);
  90. }
  91. });
  92. }
  93.  
  94. }
  95.  
  96. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement