jQuery toggleClass example

Toggle class name on click in jQuery

by Dorababu Meka

HTML

<input type="file" id="file-attach" multiple="multiple" />

JavaScript

var AttachmentsArray = [];
$("#file-attach").change(function() {
  getSelectedFiles($('#file-attach'), AttachmentsArray);
  //console.log(AttachmentsArray);
});

function getSelectedFiles(fileId, data) {
  for (var j = 0; j < fileId[0].files.length; j++) {
    readTheFile(fileId[0].files[j], data);
  }
}

function readTheFile(file, data) {
  var fileReader = new FileReader();
  fileReader.filename = file.name;
  fileReader.size = file.size;
  fileReader.onload = function(e) {
    var bits = e.target.result;
    var Object = {
      FileName: e.target.filename,
      Size: e.target.size,
      length: bits.length,
      DocumentData: _arrayBufferToBase64(bits)
    };
    data.push(Object);
    console.log(Object);
  };
  fileReader.readAsArrayBuffer(file);
}

function _arrayBufferToBase64(buffer) {
  var base64 = ''
  var bytes = new Uint8Array(buffer);
  var byteLength = bytes.byteLength
  var byteRemainder = byteLength % 3
  var mainLength = byteLength - byteRemainder
  var a, b, c, d
  var chunk
  var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  console.log(mainLength);
  // Main loop deals with bytes in chunks of 3
  for (var i = 0; i < mainLength; i = i + 3) {
    // Combine the three bytes into a single integer
    chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]

    // Use bitmasks to extract 6-bit segments from the triplet
    a = (chunk & 16515072) >> 18 // 16515072 = (2^6 - 1) << 18
    b = (chunk & 258048) >> 12 // 258048   = (2^6 - 1) << 12
    c = (chunk & 4032) >> 6 // 4032     = (2^6 - 1) << 6
    d = chunk & 63 // 63       = 2^6 - 1

    // Convert the raw binary segments to the appropriate ASCII encoding
    base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d]
  }

  // Deal with the remaining bytes and padding
  if (byteRemainder == 1) {
    chunk = bytes[mainLength]

    a = (chunk & 252) >> 2 // 252 = (2^6 - 1) << 2

    // Set the 4 least significant bits to zero
    b = (chunk &...