我想将react-native-document-picker的响应转换为base64格式。作为响应,我得到了文件的 Uri,但不幸的是没有 base64。
这是我的代码:
const openDocumentFile = async () =>{
try {
const results = await DocumentPicker.pickMultiple({
type: [DocumentPicker.types.pdf],
readContent: true
});
for (const res of [results]) {
console.log(res)
};
} catch (err) {
if (DocumentPicker.isCancel(err)) {
} else {
throw err;
}
}
} ```
你可以使用这个库
反应本机图像-base64 或者 rn-fetch-blob
import RNFetchBlob from 'rn-fetch-blob';
RNFetchBlob.fs
.readFile(filePath, 'base64')
.then((data) => {
setdata(data);
})
.catch((err) => {});
您还可以使用 react-native-fs 库。最好解码文件路径/uri。
import RNFS from 'react-native-fs';
...
const base64 = await RNFS.readFile(decodeURIComponent(filePath), 'base64');
...