import { Platform, ActionSheetIOS } from 'react-native';
import DocumentPicker from 'react-native-document-picker';
import ImagePicker from 'react-native-image-crop-picker';
import RNFS from 'react-native-fs';
For document conversion:
pickDocument = async (resolve: (payload: any) => void, reject: (payload: any) => void) => {
try {
const result = await DocumentPicker.pick({
type: [DocumentPicker.types.images, DocumentPicker.types.pdf]
});
const fileType = result[0].type;
const fileExtension = fileType.substr(fileType.indexOf('/') + 1);
const realURI = Platform.select({ android: result[0].uri, ios: decodeURI(result[0].uri), })
const b64 = await RNFS.readFile(realURI, "base64")
const filename = result[0].name.replace(/\s/g, '')
resolve({ b64, fileType, fileExtension, filename });
} catch {
reject(new Error('Action cancelled!'));
}
}
For image conversion:
pickImage = (resolve: (payload: any) => void, reject: (payload: any) => void) => {
ImagePicker.openPicker({
width: 300,
height: 400,
cropping: true,
}).then(async response => {
const { path: b64Content, mime: fileType, filename:name } = response;
const b64 = await RNFS.readFile(b64Content, "base64")
const fileExtension = String(fileType).substr(String(fileType).indexOf('/') + 1);
const filename = name.replace(/\s/g, '')
resolve({ b64, fileType, fileExtension, filename });
}).catch(({message})=>{
console.log('ERROR',message)
reject(new Error('Action cancelled!'));
});
}