Converting base64 data from json response into ms excel file using javascript

I have an app where I do a $.ajax and do a get call to an API, which returns a previously converted excel file (excel to base64); I need to re-convert this base64 data back into it's original form - i.e. into Excel file. I tried retracing the steps I took to convert the excel into base64, reversing some of them, but I'm not able to generate the original file. An excel file IS being generated, but it still has base64 data and therefore opens w/ errors and in a corrupted state. Has anyone else successfully done this?

Below is my code and fiddle link: (I didn't add the base64 json data (responseData) here since it's large, but it's on the fiddle)

var bindata = window.atob(responseData);
function DownloadExcel() {
  window.location.href = "data:application/;base64, bindata"
var blob = new Blob([responseData], {type: 'application/'});
if (window.navigator && window.navigator.msSaveBlob) {
else {
    var objectUrl = URL.createObjectURL(blob);;

Open in new window

Fiddle link:
roger vAsked:
Who is Participating?
Najam UddinCommented:
try this
function base64toBlob(b64Data, contentType, sliceSize) {
  contentType = contentType || '';
  sliceSize = sliceSize || 512;

  var byteCharacters = atob(b64Data);
  var byteArrays = [];

  for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
    var slice = byteCharacters.slice(offset, offset + sliceSize);

    var byteNumbers = new Array(slice.length);
    for (var i = 0; i < slice.length; i++) {
      byteNumbers[i] = slice.charCodeAt(i);

    var byteArray = new Uint8Array(byteNumbers);

  var blob = new Blob(byteArrays, {type: "application/"});
  return blob;

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.