React 本机 FS copyFile() 永远不会解析
React native FS copyFile() never resolves
我必须将使用 react-native-document-picker 选择的文件复制到我的应用程序的临时路径,然后读取该文件,但是当它到达 await ReactNativeFS.copyFile(realPath, tempPath);
行时,它永远不会解析。这是代码
searchAndReadFiles = async () => {
try {
const fileSelected = await DocumentPicker.pick ({
type: DocumentPicker.types.plainText,
});
const decodedURI = decodeURIComponent(fileSelected.uri);
const split = decodedURI.split('/');
const name = split.pop();
const inbox = split.pop();
const realPath = `${ReactNativeFS.TemporaryDirectoryPath}/${name}`;
const tempPath = `${ReactNativeFS.ExternalStorageDirectoryPath}/${fileSelected.name}`;
await ReactNativeFS.copyFile(realPath, tempPath);
const fileRead = await ReactNativeFS.readFile(tempPath);
} catch (err) {
console.warn(err);
}
}
所以我发现 copyFile() 方法本身解码路径,我只是将编码的 uri(与我从文档选择器 pick() 方法接收的方式相同)作为参数传递,它工作正常,谢谢。
searchAndReadFiles = async () => {
try {
const fileSelected = await DocumentPicker.pick ({
type: DocumentPicker.types.allFiles,
});
const destPath = `${ReactNativeFS.CachesDirectoryPath}/${fileSelected.name}`;
await ReactNativeFS.copyFile(fileSelected.uri, destPath);
...
我必须将使用 react-native-document-picker 选择的文件复制到我的应用程序的临时路径,然后读取该文件,但是当它到达 await ReactNativeFS.copyFile(realPath, tempPath);
行时,它永远不会解析。这是代码
searchAndReadFiles = async () => {
try {
const fileSelected = await DocumentPicker.pick ({
type: DocumentPicker.types.plainText,
});
const decodedURI = decodeURIComponent(fileSelected.uri);
const split = decodedURI.split('/');
const name = split.pop();
const inbox = split.pop();
const realPath = `${ReactNativeFS.TemporaryDirectoryPath}/${name}`;
const tempPath = `${ReactNativeFS.ExternalStorageDirectoryPath}/${fileSelected.name}`;
await ReactNativeFS.copyFile(realPath, tempPath);
const fileRead = await ReactNativeFS.readFile(tempPath);
} catch (err) {
console.warn(err);
}
}
所以我发现 copyFile() 方法本身解码路径,我只是将编码的 uri(与我从文档选择器 pick() 方法接收的方式相同)作为参数传递,它工作正常,谢谢。
searchAndReadFiles = async () => {
try {
const fileSelected = await DocumentPicker.pick ({
type: DocumentPicker.types.allFiles,
});
const destPath = `${ReactNativeFS.CachesDirectoryPath}/${fileSelected.name}`;
await ReactNativeFS.copyFile(fileSelected.uri, destPath);
...