I am converting the audio MP3 file and storing it as base64 in database using WEB API, now I am fetching the base64 file using ajax and I am getting the base64, the problem is how can I convert the base64 file back to mp3 file and play it using JavaScript.
This if for demo I am using input file, I am getting base64 file from server
JavaScript
x
2
1
<input type='file' onchange='openFile(event)' id="EdituserProfileImage">
2
JavaScript
1
20
20
1
var fileName;
2
var filetype;
3
var filesize;
4
var VoiceBase64;
5
var openFile = function (event) {
6
var input = event.target;
7
fileName = input.files[0].name;
8
filetype = input.files[0].type;
9
filesize = input.files[0].size;
10
console.log(input);
11
console.log(fileName);
12
var reader = new FileReader();
13
reader.onload = function (evt) {
14
var voiceInBinay = evt.target.result;
15
VoiceBase64 = btoa(voiceInBinay);
16
contvertBase64toBinaray(VoiceBase64);
17
};
18
reader.readAsBinaryString(input.files[0]);
19
};
20
This function “contvertBase64toBinaray” using for converting base64 to Binary, I have binary file, need to save as mp3 from this below binary
JavaScript
1
4
1
function contvertBase64toBinaray(VoiceBase64) {
2
var audiofile = atob(VoiceBase64)
3
};
4
Advertisement
Answer
Use window.atob
function to decode your base 64 data.
This question shows you how you can play the mp3 in JS.