Skip to main content

Keeping a local copy of the picked files

keepLocalCopy makes the file available in the app's storage. The behavior is different on iOS and Android:

This method opens an InputStream pointing to the picked content:// uri (from both Open and Import modes) and stores its bytes into a file - i.e. it can be used to "convert" a content:// Uri into a local file. This file's location is determined by the destination parameter.

It also "converts" virtual files (such as Google docs or sheets) into local files.


note

For each call of keepLocalCopy, a new unique directory is created in the app's storage, and the files are placed into it.

This way, the files are isolated and subsequent calls to keepLocalCopy with the same file names do not overwrite the previous files.

When writing to the filesystem, path traversal vulnerability is prevented. Writing files outside the intended destination will error.

Also note that for some common use cases, such as uploading the picked file to a server, you may not need to call keepLocalCopy. This will be clarified in a later docs update.

Example: keeping a local copy of the picked file
import { pick, keepLocalCopy } from '@react-native-documents/picker'
return (
<Button
title="single file import, and ensure it is available in the local storage"
onPress={async () => {
try {
const [{ name, uri }] = await pick()

const [copyResult] = await keepLocalCopy({
files: [
{
uri,
fileName: name ?? 'fallback-name',
},
],
destination: 'documentDirectory',
})
if (copyResult.status === 'success') {
// do something
}
} catch (err) {
// see error handling
}
}}
/>
)