diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/components/FileUpload/FileUpload.tsx | 4 | ||||
-rw-r--r-- | src/utils/files.ts | 15 |
2 files changed, 17 insertions, 2 deletions
diff --git a/src/components/FileUpload/FileUpload.tsx b/src/components/FileUpload/FileUpload.tsx index 6a31656..d455f62 100644 --- a/src/components/FileUpload/FileUpload.tsx +++ b/src/components/FileUpload/FileUpload.tsx @@ -1,7 +1,7 @@ import React, { useRef } from 'react'; -import { utils } from 'react-sage'; import Button from '@material-ui/core/Button'; import CloudUpload from '@material-ui/icons/CloudUpload'; +import { getLocalFileUrl } from '../../utils/files'; interface PropTypes { callback: (fileUrl: string, file: File) => void; @@ -15,7 +15,7 @@ const FileUpload: React.FC<PropTypes> = ({ callback, children }) => { const files = event.target?.files; if (files?.length) { const file = files[0]; - utils.loadFile(file).then(url => callback(url, file)); + getLocalFileUrl(file).then(url => callback(url, file)); }; }; diff --git a/src/utils/files.ts b/src/utils/files.ts new file mode 100644 index 0000000..98c342c --- /dev/null +++ b/src/utils/files.ts @@ -0,0 +1,15 @@ +export const getLocalFileUrl = (file: File): Promise<string> => { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + + reader.addEventListener('load', () => { + if (typeof reader.result === 'string') resolve(reader.result); + }, false); + + reader.addEventListener('error', () => { + reject(new Error('Error reading the file')) + }, false ); + + if (file) reader.readAsDataURL(file); + }); +}; |