Search code examples
javascriptbase64blobwav

how can i convert .wav file from url to base64 in client-side javascript?


I have .wav file, placed on "http://yy.zz/1.wav". How can i fetch it with base64 encoding on client-side javascript?

getFile('http://yy.zz/1.wav', (file) => {
  console.log(getBase64File(file)); // profit
});

Solution

  • If the file is on the same domain or you have CORS enabled on the external site, you can do the following:

    $.get("http://localhost/content/sound/hello.wav", function(file){
       let fileInBase64Format = btoa(unescape(encodeURIComponent(file)));
       console.log(fileInBase64Format);
    });