question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

writeFile returning success after writing 0 bytes

See original GitHub issue

Hello! Thank you for the very useful library. I have run into some trouble

  • RN: 0.56.0
  • rn-fetch-blob: 0.10.13
  • iOS simulator

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

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:10

github_iconTop GitHub Comments

9reactions
ddaeschlercommented, Sep 22, 2018

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.

2reactions
soutotcommented, Aug 18, 2021

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.

Thank you so much! I’ve been struggling on this for hours

ref?.current?.toDataURL((data) => {
      const filename =  'some-image-stuff.png'
      const imagePath = `${RNFetchBlob.fs.dirs.CacheDir}/${filename}`

      RNFetchBlob.fs
        .writeFile(
           imagePath,
           data.replace(/\r?\n|\r/g, ''), // THE FIX
           'base64'
        )
        .then(() => {
         CameraRoll.save(imagePath)
            .then(() => {
                console.log('Success')
              })
            .catch((saveError) => console.log({saveError}))
        })
        .catch((writeError) => console.log({writeError))
    })
Read more comments on GitHub >

github_iconTop Results From Across the Web

WriteFile returns 0 but writes bytes - MSDN - Microsoft
hello. I am using a thread environment to write and read asynchronically from a com port: Here is the logic: 1. try to...
Read more >
Node Js writeFile always return 0 bytes when looping through ...
I have about more than 7.000 Base64 data that I want to save into a file using NodeJs, I can do this just...
Read more >
WriteFile() returns 1, but writes 0 bytes
I am using WriteFile() to write to a serial port on an embedded CE device without overlapping. Very intermittantly, WriteFile() will return 0...
Read more >
write
If write() is interrupted by a signal after it successfully writes some data, it will return the number of bytes written. If the...
Read more >
write(2): to file descriptor - Linux man page - Die.net
If count is zero and fd refers to a regular file, then write() may return a failure status if one of the errors...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found