Open skyride99 opened 8 years ago
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 Blob
objects does not have correct functionality in react native ?
same question here.
I always use downloadFile()
for dealing with binary data. It is doing an asynchronous GET-request to a specific URL and stores the response binary on device.
@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:
const buffer = fs.readFileSync('./test.png');
for (let i = 0; i < buffer.length; i++) {
const num = buffer[i];
const str = String.fromCharCode(num);
fs.appendFileSync('./test1.png', str, { encoding: 'ascii' });
}
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. Then the following code will work
RNFS.writeFile(filename, response.data, 'base64')
Do we have solution for this ? I want to download the csv file generated in backend which is recived in app in blob data fomat.
You can use URL.createObjectURL(blob);
, which will save the blob locally and return a URL which you can later use to render the blob (in my case it's a PDF).
You can use
URL.createObjectURL(blob);
, which will save the blob locally and return a URL which you can later use to render the blob (in my case it's a PDF).
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)
You can use
URL.createObjectURL(blob);
, which will save the blob locally and return a URL which you can later use to render the blob (in my case it's a PDF).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 blob
was the built-in RNs Blob
object, but if you have a string I suppose you could use the writeFile
utility from react-native-fs
, like in this guide
You can use
URL.createObjectURL(blob);
, which will save the blob locally and return a URL which you can later use to render the blob (in my case it's a PDF).
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
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); }); },