如何修复:我想获得绝对路径,但在react native中使用文档选择器时未定义文件路径。
import { StyleSheet, Text, View,Button } from 'react-native'
import React from 'react'
import DocumentPicker from 'react-native-document-picker'
import RNFetchBlob from 'rn-fetch-blob'
import Pdf from 'react-native-open-pdf';
const openPdfex = () => {
const openFile = () => {
const res = DocumentPicker.pick({
type: [DocumentPicker.types.allFiles],
});
console.log("absolute path =====> " ,res.uri);
}
return (
<View>
<Text>openPdf</Text>
<Button title='Open PDF' onPress={openFile}></Button>
</View>
)
}
试试这个
const openFile = async () => {
try {
const res = await DocumentPicker.pick({
type: [DocumentPicker.types.allFiles],
});
console.log('absolute path =====> ', JSON.stringify(res[0]['uri']));
} catch (error) {
console.log(error);
}}
在文档中,它声明DocumentPicker.pick()返回一个承诺,因为您没有解析承诺,因此无法从中获取uri。
const openFile = async () => {
try {
const res = DocumentPicker.pick({
type: [DocumentPicker.types.allFiles],
});
console.log('absolute path =====> ', res.uri);
} catch (error) {
console.log(error);
}
};
试试,看看它的工作原理。
用DocumentPicker.pickSingle
代替DocumentPicker.pick