diff options
author | eug-vs <eug-vs@keemail.me> | 2020-08-22 13:54:56 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-08-22 13:54:56 +0300 |
commit | 668c9f4841e7118b98bb31d8e68640689be99830 (patch) | |
tree | 692c66b7fee252bba4466104ebd4c8fff02fcf19 /src/utils/getLocalFileUrl.ts | |
parent | fdf826bf38b967d2f4346e9ee5950e157f0f0beb (diff) | |
download | which-ui-668c9f4841e7118b98bb31d8e68640689be99830.tar.gz |
refactor!: simplify file operations
Diffstat (limited to 'src/utils/getLocalFileUrl.ts')
-rw-r--r-- | src/utils/getLocalFileUrl.ts | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/utils/getLocalFileUrl.ts b/src/utils/getLocalFileUrl.ts new file mode 100644 index 0000000..1469804 --- /dev/null +++ b/src/utils/getLocalFileUrl.ts @@ -0,0 +1,15 @@ +export default (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); + }); +}; |