How can I get base64 string from filecontent in widget

How can I get base64 string from filecontent in widget

Hi,

I have a react js widget which has the signature pad. Now, I am saving the signature in signature field in zoho creator form. If I open the edit report record in widget then I want to display the Signature back in signature field. 

I am using readFile api to get the filecontent but I am not able to convert it into base64 string properly. I have below code. What is the correct code to get base64 string of image in widget.

try {
const filepath = extractFilePath(zohoFilePath);
if (!filepath) throw new Error("Invalid file path");
const config = {
file_path: filepath,
id: recordId,
report_name: 'All_Assessments',
field_name: 'Draw'
};
const response = await window.ZOHO.CREATOR.FILE.readFile(config);
if (!response) return null;

const chunkSize = 0x8000;
let binary = '';
for (let i = 0; i < response.length; i += chunkSize) {
const chunk = response.slice(i, i + chunkSize);
binary += String.fromCharCode.apply(null, Array.from(chunk).map(c => c.charCodeAt(0) & 0xff));
}
const base64 = btoa(binary);
const dataUrl = `data:image/png;base64,${base64}`;
console.log("base64 - ", dataUrl);
return dataUrl;
} catch (err) {
console.error("Error converting file:", err);
return null;
}