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.

bad base-64 fs.readStream

See original GitHub issue

Info

"rn-fetch-blob": "^0.11.2",
"react-native": "^0.60.5",
platform: both

Issue: Cant write file after it read from stream as base64. My test file is image (jpg)

YellowBox.js:71 Possible Unhandled Promise Rejection (id: 0):
Error: bad base-64
Error: bad base-64
    at createErrorFromErrorData 

What: I want to read from stream as base64 string and save it to new file using fs.createFile

const { fs } = RNFetchBlob;

  fs
    .exists(file.image.path)
    .then(exist => {
      console.log('Send file socket exist', exist);

      if (exist) {
        console.log('index file exists');

        fs
          .readStream(file.image.path, 'base64', 102400)
          .then(stream => {
            stream.open();

            let result = '';

            stream.onData(chunk => {
              result += chunk;

              // console.log('Chunk', chunk);
              // console.log('~~~~~~~~~~~~');
            });
            stream.onError(error => {
              console.log('ERRRRORRRR');
              console.log(error);
            });
            stream.onEnd(() => {
              const dirs = RNFetchBlob.fs.dirs;

              // .writeFile(
              RNFetchBlob.fs
                .createFile(
                  dirs.DownloadDir + '/tesssssssst.jpg',
                  result,
                  'base64'
                )
                .then(() => {});
            });
          })
          .catch(err => {
            console.log(err);
          });
      } else {
        console.log('index file NOT exists');
      }
    })
    .catch(err => {
      console.log(err);
    });

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9

github_iconTop GitHub Comments

4reactions
NanduKkdcommented, Jul 28, 2020

I changed the buffersize from 102400 to 102399 and it worked like magic! 102401 didn’t work but 102402 worked.

0reactions
khayymcommented, Sep 19, 2022

const stream = await RNFetchBlob.fs.readStream('(file path)', 'base64'); let fileContentsBuffer = Buffer.from('', 'base64'); await new Promise((resolve, reject) => { stream.onError(error => reject(error)); stream.open(); stream.onData(chunk => (fileContentsBuffer = Buffer.concat([fileContentsBuffer, Buffer.from(chunk, 'base64')]))); stream.onEnd(() => resolve()); });

try this. https://github.com/joltup/rn-fetch-blob/issues/315

Read more comments on GitHub >

github_iconTop Results From Across the Web

fs.writeFile corrupt, using base64 string - node.js
The problem is that req.body.image contains a Data URL, which is more than just the image body. The base64 image data starts after...
Read more >
Node.js v19.3.0 Documentation
When creating a Buffer from a string, this encoding will also correctly accept regular base64-encoded strings. When encoding a Buffer to a string,...
Read more >
@zerobytellc/rn-fetch-blob - npm
This API creates a file stream, rather than convert entire data into BASE64 encoded string. It's handy when processing large files. When calling ......
Read more >
Using the writeFileSync method in Node.js - LogRocket Blog
writeFileSync function can help you easily get started with the · writeFile function because they have similar parameters, and the only ...
Read more >
Top 5 base64-arraybuffer Code Examples - Snyk
Learn more about how to use base64-arraybuffer, ... encoding: 'binary', }; const data = Buffer.from(base64.decode(saveFileReq.data)); fs.writeFile(save.
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