Hello! Thank you for the very useful library. I have run into some trouble
fs.writeFile with base64 data is returning without error, but writing zero bytes to the file
this.svg.toDataURL((data) => {
console.log(data);
const qrFile = RNFetchBlob.fs.dirs.CacheDir + '/qrbadge.png';
RNFetchBlob.fs.writeFile(qrFile, data, 'base64')
.then((len) => {
console.log('qr image to ' + qrFile + ' with len ' + len);
self.setState({qrImage: qrFile});
})
.catch((reason) => {
console.log('ex: ' + reason);
})
});
On the console I see:
[base64 data which I've pasted online and it yields the PNG image as expected]
qr image to /Users/daviddaeschler/Library/Developer/CoreSimulator/Devices/90DDC9E1-05D6-43C0-BAB2-603A9BDB341A/data/Containers/Data/Application/D383B543-0DD3-4663-A791-8DA07BD6A15B/Library/Caches/qrbadge.png with len 0
I've also confirmed the file exists on the filesystem and is indeed zero bytes
I figured this out. The base64 output from the other library I'm using was adding newline chars to the output. This must've caused the iOS writeFile method to silently fail to decode the base64.
It looks like line breaks in base64 data are widely accepted, so I guess a fix for the problem at the rn-fetch-blob level might be simply to strip line breaks before feeding it to NSData for decoding.
I am having a similar problem here. The operation finish without erros but the file written has 0 bytes. In other hand, I can share this base64 pdf with RNShare without problems for example. When I try write a different base64 pdf everything works as expected.
Same probem occuring with me
guys if i use rn-fetch-blob to write file, the only way to output is to use base64? can i use the path way and output like something like this?
<Image
style={{width: '100%', height: 200}}
source={{uri: res.path()}}
/>
let dirs = RNFetchBlob.fs.dirs
RNFetchBlob
.config({
// response data will be saved to this path if it has access right.
path : dirs.DocumentDir + '/path-to-file.anything'
})
.fetch('GET', 'uri', {
//some headers ..
})
.then((res) => {
// the path should be dirs.DocumentDir + 'path-to-file.anything'
console.log('The file saved to ', res.path())
//
})
@nonoyek You can try this, But it is not working on android properly 馃憥
I also have the same issue,
let dirs = RNFetchBlob.fs.dirs
let fileName = response.fileName ? response.fileName : ${Math.floor(new Date())}.jpg;
RNFetchBlob.fs.writeFile(dirs.DocumentDir + /projectImages/${fileName}, response.uri, 'uri')
.then((len)=>{
console.log('qr image to ' + dirs.DocumentDir + /projectImages/${fileName} + ' with len ' + len);
//with len -1
})
It saves the file to the location, but zerobytes in Size
I have the same issue..
I remove "data:image/png;base64," first before converting to jpg, and it work.
imageData = imageData.substring("data:image/png;base64,".length, imageData.length);
const imagePath = isIos ? ${RNFetchBlob.fs.dirs.CacheDir}/${inv}.jpg : ${RNFetchBlob.fs.dirs.DCIMDir}/${inv}.png;
RNFetchBlob.fs.writeFile(imagePath, imageData, 'base64')
.then((res) => {
console.log('Image converted to jpg and saved at ' + imagePath);
});
Most helpful comment
I figured this out. The base64 output from the other library I'm using was adding newline chars to the output. This must've caused the iOS writeFile method to silently fail to decode the base64.
It looks like line breaks in base64 data are widely accepted, so I guess a fix for the problem at the rn-fetch-blob level might be simply to strip line breaks before feeding it to NSData for decoding.