This works for me. tested in Android and iOS for camera and gallary
const os = Platform.OS
let data = new FormData();
if (os == 'android') {
const fileName = new Date().getTime() + getExtention(mime);
let fileInfo = '';
await RNFS.copyFile(uri, RNFS.CachesDirectoryPath + '/' + fileName).catch(e => {
fileInfo = undefined;
});
if (!fileInfo) {
fileDetail = await RNFS
.stat(RNFS.CachesDirectoryPath + '/' + fileName)
.catch(e => {});
data.append('file', {
name: getFilename(fileDetail.path),
type: payload.file.type,
uri: 'file://' + fileDetail.path,
});
}
} else {
let localPath= uri;
if (!localPath.includes('private')) {
localPath = localPath.replace('/var', '/private/var');
}
data.append('file', {
name: getFilename(localPath),
type: mime,
uri: localPath.replace('file://', ''),
});
}
export const getFilename = url => {
return url.substr(url.lastIndexOf('/') + 1);
};
export const getExtention = mime => {
switch (mime) {
case 'application/pdf':
return '.pdf';
case 'image/jpeg':
return '.jpg';
case 'image/jpg':
return '.jpg';
case 'image/png':
return '.png';
default:
return '.jpg';
}
};