sci_helper / utils /file.ts
rachel521's picture
Upload 12 files
0d3c552 verified
Raw
History Blame Contribute Delete
613 Bytes
export const fileToBase64 = (file: File): Promise<string> => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
const result = reader.result as string;
// The result is "data:application/pdf;base64,xxxxx..."
// We only need the part after the comma.
const base64String = result.split(',')[1];
if (base64String) {
resolve(base64String);
} else {
reject(new Error("Failed to read file as base64."));
}
};
reader.onerror = (error) => reject(error);
});
};