Sunday, January 1, 2023

Convert XML string to blob using Javascript

 This post explain how to convet to XML string to Blob and you can sent blob to servers.


   convertXMLStringToblob = (xmlstring) => {
      // Convert xml string to base64data
     let xmlval = new DOMParser().parseFromString(xmlstring, 'application/xml');
     let base64Data = window.btoa((new XMLSerializer()).serializeToString(xmlval));
   
     // Convert base64data to blob
     const byteCharacters = window.atob(base64Data);
     const byteNumbers = new Array(byteCharacters.length);
     for (let i = 0; i < byteCharacters.length; i++) {
       byteNumbers[i] = byteCharacters.charCodeAt(i);
     }
     const byteArray = new Uint8Array(byteNumbers);
     return new Blob([byteArray], {type:'application/xml'});
   }

Sample file



No comments:

Post a Comment