Skip to content

Instantly share code, notes, and snippets.

@sijad
Created January 22, 2019 18:57
Show Gist options
  • Save sijad/cd7410c9eb5229831793b3518327315b to your computer and use it in GitHub Desktop.
Save sijad/cd7410c9eb5229831793b3518327315b to your computer and use it in GitHub Desktop.
react native fetch and convert file to base64
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