Download file with extension for internet explorer and chrome using javascript

SOLVED Problem Statement :
Internet explorer download file does not shows file extension

Solution : java script code that append file type extension

function save() {
  var fileUrl = "${fileUrl}";
  var fileName = "${fileName}";
  var mimeType = "${mimeType}";

  var ie = navigator.userAgent.match(/MSIE\s([\d.]+)/), ie11 = navigator.userAgent
    .match(/Trident\/7.0/)
    && navigator.userAgent.match(/rv:11/), ieEDGE = navigator.userAgent
    .match(/Edge/g), ieVer = (ie ? ie[1] : (ie11 ? 11
    : (ieEDGE ? 12 : -1)));

  if (ie && ieVer < 10) {
   console.log("No blobs on IE ver<10");
   return;
  }
  if (ieVer > -1) {
   console.log(" blobs on IE ver>10");
   //window.open(fileUrl,"_self");        

   var xhr = new XMLHttpRequest();
   xhr.open('GET', fileUrl, true);
   xhr.responseType = 'blob';
   xhr.onload = function(e) {
    if (this.status == 200) {
     var myBlob = this.response;
     downloadFileForIE(myBlob, fileName, mimeType);
    }
   };
   xhr.send();
  }

  if (!window.Blob) {
   window.open(fileUrl);
  } else {
   var xhr = new XMLHttpRequest();
   xhr.responseType = 'blob';
   xhr.onload = function() {
    var a = document.createElement('a');
    a.href = window.URL.createObjectURL(xhr.response); // xhr.response is a blob
    a.download = fileName; // Set the file name.
    a.style.display = 'none';
    document.body.appendChild(a);
    a.click();
    delete a;
   };
   xhr.open('GET', fileUrl);
   xhr.send();
  }
 }
 function downloadFileForIE(blob, fileName, mimeType) {
  // It is necessary to create a new blob object with mime-type explicitly set
  // These changes will be only for IE browser
  var newBlob = new Blob([ blob ], {
   type : mimeType
  }, {
   name : fileName
  })
  var formData = new FormData();
  // IE doesn't allow using a blob object directly as link href
  // instead it is necessary to use msSaveOrOpenBlob
  if (window.navigator && window.navigator.msSaveOrOpenBlob) {
   window.navigator.msSaveOrOpenBlob(newBlob, fileName
     + getFileExtension(mimeType, fileName
       + getFileExtension(mimeType)));
   return;
  }

 }
 function getFileExtension(mimetype) {
  var text;

  switch (mimetype) {
  case "application/pdf":
   text = ".pdf";
   break;
  case "image/bmp":
   text = ".bmp";
   break;
  case "image/gif":
   text = ".gif";
   break;
  case "image/jpeg":
   text = ".jpeg";
   break;
  case "image/jpg":
   text = ".jpg";
   break;
  case "image/pjpeg":
   text = ".pjpeg";
   break;
  case "image/png":
   text = ".png";
   break;
  case "image/tiff":
   text = ".tiff";
   break;
  case "application/vnd.ms-outlook":
   text = ".msg";
   break;
  case "application/x-filenet-filetype-msg":
   text = ".msg";
   break;
  case "message/rfc822":
   text = ".eml";
   break;
  case "application/vnd.ms-excel":
   text = ".xls";
   break;
  case "application/vnd.ms-powerpoint":
   text = ".ppt";
   break;
  case "application/msword":
   text = ".doc";
   break;
  case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
   text = ".pptx";
   break;
  case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
   text = ".xlsx";
   break;
  case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
   text = ".docx";
   break;
  case "application/octet-stream":
   text = "";
   break;

  default:
   text = "";
  }
  return text;
 }

0 comments:

Post a Comment