Created
January 22, 2019 18:57
-
-
Save sijad/cd7410c9eb5229831793b3518327315b to your computer and use it in GitHub Desktop.
react native fetch and convert file to base64
This file contains 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
interface FileReader { | |
readAsDataURL(blob: Blob): string; | |
onloadend(): void; | |
result: string; | |
} | |
declare var FileReader: { | |
prototype: FileReader; | |
new (): FileReader; | |
}; | |
function toDataURL(url: string, callback: (uri: string) => void) { | |
const xhr = new XMLHttpRequest(); | |
xhr.withCredentials = true; | |
xhr.onload = function() { | |
const reader = new FileReader(); | |
reader.onloadend = function() { | |
callback(reader.result); | |
}; | |
reader.readAsDataURL(xhr.response); | |
}; | |
xhr.open('GET', url); | |
xhr.responseType = 'blob'; | |
xhr.send(null); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment