Created
October 15, 2019 13:31
-
-
Save srivatsav/e1cb13584672435c47cfcf42bb5be96e to your computer and use it in GitHub Desktop.
Blob to base64 converter
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
<View style={{ flex: 1 }}> | |
<Button | |
title="download" | |
onPress={() => { | |
fetchFileAsPdf({...request}) | |
.then((response) => { | |
// create a new file reader. | |
const fileReader = new FileReader(); | |
// use readAsDataURL to read the contents of a Blob/File. | |
fileReader.readAsDataURL(response); | |
// triggered when the read operaiton is finished. | |
fileReader.onloadend = function () { | |
const base64data = fileReader.result; | |
// Remove the preamble data:*/*;base64, to get the exact encoded base64. Else it won't work. | |
const filteredBase64 = base64data.split(',')[1]; | |
const { fs } = RNFetchBlob; | |
const { DownloadDir } = fs.dirs; | |
// choose the directory and write the base64 data to a file. | |
RNFetchBlob.fs | |
.writeFile(`${DownloadDir}/Invoice.pdf`, filteredBase64, 'base64') | |
.then(res => console.log('File :: ', res)) | |
.catch(err => console.log('Error in writing file :: ', err)); | |
}; | |
}) | |
.catch(err => console.log('Error in downloading :: ', err)); | |
}} | |
/> | |
</View> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment