joltup / rn-fetch-blob

A project committed to making file access and data transfer easier, efficient for React Native developers.
MIT License
2.84k stars 784 forks source link

How to get File path from the document directory in iOS and convert that file to Base64. #639

Open gopikasireddy202 opened 4 years ago

gopikasireddy202 commented 4 years ago

Hi Team,

Please help me to get the file path from the document in the iOS and also convert the file to Base64 format. Please find my below code and also find the screenshots. File Error File Path


async pickupDocuments() { try { const results = await DocumentPicker.pickMultiple({ type: [DocumentPicker.types.allFiles], }); for (const res of results) {

            let datapath = Platform.OS === 'ios' ? res.uri.replace("file://", "") : res.uri
            alert(" FILE PATH IS " + datapath)

            RNFetchBlob.fs.readFile(datapath, 'base64')
                .then((data) => {
                    let base = data.replace(/\s+/g, '')
                    alert(" BASE64 IS " + base)

                })
                .catch((errorMessage, statusCode) => {
                    alert(" ERROR MESSAGE IS " + errorMessage)
                    // error handling
                })

        }

    }
    catch (err) {
        if (DocumentPicker.isCancel(err)) {
            alert("User Cancel")
        } else {
            //   throw err;
            alert(err)
        }
    }
}