Skip to content
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

Write file doesn't save base64 string on IOS, how to fix it ? #869

Open
andriiradkevych opened this issue May 5, 2024 · 2 comments
Open

Comments

@andriiradkevych
Copy link

import RNFetchBlob from 'rn-fetch-blob';
import {Platform} from 'react-native';

import {generateUuid} from './helpers';

export const saveImageBase64 = async (
  base64String: string,
  successCallback: () => void,
) => {
  const fileName = `${generateUuid()}.png`;
  const dirs = RNFetchBlob.fs.dirs;

  const Base64Code = base64String.split('data:image/png;base64,');

  const filePath = `${
    Platform.OS === 'android' ? dirs.DownloadDir : dirs.DocumentDir
  }/${fileName}`;

  RNFetchBlob.fs
    .writeFile(filePath, Base64Code[1], 'base64')
    .then(() => {
      if (Platform.OS === 'ios') {
        RNFetchBlob.ios.openDocument(filePath);
      } else {
        successCallback();
      }
    })
};

What I have here is saveImageBase64 function , that perfectly works on Android , it saves my image , and I'm happy , but on ios , function doesn't send any error , seems like all is working well , but when I try to open this image by RNFetchBlob.ios.openDocument(filePath); , it just show me :

2024-05-05 17 24 23

what does it mean ? and could someone show me how can I save my file , and open it , or just save on ios , because I don't see that anything saved to my Document dir

@andriiradkevych
Copy link
Author

  const baseWithoutSpaces = Base64Code[1].replace(/\r?\n|\r/g, '');

  RNFetchBlob.fs
    .writeFile(filePath, baseWithoutSpaces, 'base64')
    

Found fix , hope it will help someone

@rampanwar123
Copy link

rampanwar123 commented Jul 30, 2024

const baseWithoutSpaces = Base64Code[1].replace(/\r?\n|\r/g, '');

Thanks @andriiradkevych 🤟🏻, its working for me Also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants