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;
}

    Access your files securely from anywhere







                            Zoho Developer Community




                                                  • Desk Community Learning Series


                                                  • Digest


                                                  • Functions


                                                  • Meetups


                                                  • Kbase


                                                  • Resources


                                                  • Glossary


                                                  • Desk Marketplace


                                                  • MVP Corner


                                                  • Word of the Day


                                                  • Ask the Experts





                                                            Manage your brands on social media



                                                                  Zoho TeamInbox Resources



                                                                      Zoho CRM Plus Resources

                                                                        Zoho Books Resources


                                                                          Zoho Subscriptions Resources

                                                                            Zoho Projects Resources


                                                                              Zoho Sprints Resources


                                                                                Qntrl Resources


                                                                                  Zoho Creator Resources



                                                                                      Zoho CRM Resources

                                                                                      • CRM Community Learning Series

                                                                                        CRM Community Learning Series


                                                                                      • Kaizen

                                                                                        Kaizen

                                                                                      • Functions

                                                                                        Functions

                                                                                      • Meetups

                                                                                        Meetups

                                                                                      • Kbase

                                                                                        Kbase

                                                                                      • Resources

                                                                                        Resources

                                                                                      • Digest

                                                                                        Digest

                                                                                      • CRM Marketplace

                                                                                        CRM Marketplace

                                                                                      • MVP Corner

                                                                                        MVP Corner









                                                                                          Design. Discuss. Deliver.

                                                                                          Create visually engaging stories with Zoho Show.

                                                                                          Get Started Now


                                                                                            Zoho Show Resources

                                                                                              Zoho Writer

                                                                                              Get Started. Write Away!

                                                                                              Writer is a powerful online word processor, designed for collaborative work.

                                                                                                Zoho CRM コンテンツ



                                                                                                  Nederlandse Hulpbronnen


                                                                                                      ご検討中の方