From 28ff90b8bf01f8221b82670ec72e53ae89d39a40 Mon Sep 17 00:00:00 2001 From: Val Erastov Date: Sat, 9 Jul 2022 13:44:22 -0700 Subject: [PATCH] support binary files in the file widget --- modules/ui/components/controls/FileControl.tsx | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/modules/ui/components/controls/FileControl.tsx b/modules/ui/components/controls/FileControl.tsx index 6669187c..d96b33d4 100644 --- a/modules/ui/components/controls/FileControl.tsx +++ b/modules/ui/components/controls/FileControl.tsx @@ -6,6 +6,11 @@ export interface LocalFile { content: string; } +export function getBase64FromDataUrl(dataUrl: string): string { + const commaIndex = dataUrl.indexOf(','); + return dataUrl.substring(commaIndex + 1, dataUrl.length); +} + export default class FileControl extends React.Component { render() { @@ -19,12 +24,15 @@ export default class FileControl extends React.Component { const file = files[0]; - file.text().then(content => { + const reader = new FileReader(); + reader.onload = evt => { + const dataUrl = evt.target.result as string; onChange({ fileName: name, - content + content: dataUrl }) - }); + }; + reader.readAsDataURL(file); } return