1+ import * as DocumentPicker from 'expo-document-picker' ;
12import { useState } from 'react' ;
2- import { Platform } from 'react-native' ;
3- import DocumentPicker from 'react-native-document-picker' ;
4- import type { DocumentPickerResponse } from 'react-native-document-picker' ;
53
64export interface SelectedFile {
75 uri : string ;
@@ -17,37 +15,20 @@ const useFilePicker = (): {
1715 const [ filePickerError , setError ] = useState < string | null > ( null ) ;
1816
1917 const pickFile = async ( ) => {
20- let result : DocumentPickerResponse | null = null ;
21- let fileDelimiter : string | null = null ;
2218 try {
23- if ( Platform . OS === 'ios' ) {
24- fileDelimiter = '%2F' ;
25- result = await DocumentPicker . pickSingle ( {
26- allowMultiSelection : false ,
27- type : [ 'public.data' ] ,
28- copyTo : 'cachesDirectory' ,
29- } ) ;
30- }
31- if ( Platform . OS === 'android' ) {
32- fileDelimiter = '/' ;
33- result = await DocumentPicker . pickSingle ( {
34- allowMultiSelection : false ,
35- type : [ '*/*' ] ,
36- } ) ;
37- }
38-
39- if ( result == null || fileDelimiter == null ) {
40- throw new Error ( 'Failed to pick a file, is your OS supported?' ) ;
41- }
19+ const pickedFile = await DocumentPicker . getDocumentAsync ( ) ;
4220
43- const uri = result . fileCopyUri ? result . fileCopyUri : result . uri ;
44- setSelectedFile ( { uri, name : uri . split ( fileDelimiter ) . slice ( - 1 ) [ 0 ] } ) ;
45- // eslint-disable-next-line @typescript-eslint/no-explicit-any
46- } catch ( err : any ) {
47- if ( ! DocumentPicker . isCancel ( err ) ) {
48- setSelectedFile ( null ) ;
49- setError ( err . message ) ;
21+ const assets = pickedFile . assets ;
22+ if ( ! assets ) {
23+ return ;
5024 }
25+ const file = assets [ 0 ] ;
26+ setSelectedFile ( {
27+ uri : file . uri ,
28+ name : file . name || '' ,
29+ } ) ;
30+ } catch ( error ) {
31+ setError ( JSON . stringify ( error ) ) ;
5132 }
5233 } ;
5334
0 commit comments