-
Notifications
You must be signed in to change notification settings - Fork 990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to save blob or binary data #69
Comments
Have the same issue. Is there a solution? |
Same, I've got a blob but i've no idea how to write it to disk |
I'm not sure but, according to this issue, it seems |
same question here. |
I always use |
@itinance in my particular case, the binary content depends on the request, so I don't have the concept of one URL for the data. |
have you tried to use 'ascii' to encode you binary data. and write the ascii string to file. Here is a sample in node runtime:
|
ASCII is not better fit for large data as it will affect performance exponentially. Better to use base64 or utf8 they are faster enough. |
Use RNFetchBlob.fetch() instead of the fetch API.
|
Do we have solution for this ? |
You can use |
Do you have more code example? I have a base64 string of an image and I want to write it locally as an image (binary) |
Well, in my case |
This gives [Error: Cannot create URL for blob!] |
this is how i do it: import fs from "react-native-fs"
const blob_to_data_url = async (blob)=>(new Promise((res, rej)=>{
const reader = new FileReader();
reader.onloadend = () => {
res(reader.result);
};
reader.onerror = (error) => {
rej(error);
};
reader.readAsDataURL(blob);
}));
// blob to data url link
const persist_blob = async (file_path, blob)=>(
blob_to_data_url(blob)
.then(data_url => data_url.split(",")[1])
.then(b64 => fs.writeFile(file_path, b64, 'base64'))
);
// if you write a raw base64 string as type 'base64' it allows you to save blobs (closest thing to a buffer frontend js has)
const read_blob = async (file_path)=>(
fetch(`file://${file_path}`).then(x => x.blob())
);
// you can fetch local files in react native this has worked for me so far |
Without File Reader and Fetch Api
|
response is a blob....
writeBlob(response){
return RNFS.writeFile(this.path + '/' + 'first.pdf', response, 'base64')
.then((success) => {
console.log('FILE WRITTEN!');
})
.catch((err) => {
console.log(err.message);
});
},
The text was updated successfully, but these errors were encountered: