Created
February 8, 2021 13:13
-
-
Save FFKL/e3d2a26c36657be3b7fb69c1dc209d8b to your computer and use it in GitHub Desktop.
Wrap FileReader to Promise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function convertFileToBase64(file: File): Promise<string> { | |
return new Promise((resolve, reject) => { | |
const reader: FileReader = new FileReader(); | |
reader.onload = () => { | |
if (typeof reader.result === 'string') { | |
resolve(reader.result); | |
} else { | |
reject(new Error(`Reading file result should be base64 string. Result: ${reader.result}`)); | |
} | |
}; | |
reader.onerror = error => reject(error); | |
reader.readAsDataURL(file); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment