Document picker

DocumentPicker opens the system's file picker, bound to expo-document-picker: the Files app's browser on iOS and the storage access framework on Android.

Import
import { DocumentPicker } from '@ng-native/expo';
Inject
inject(DocumentPicker)

The options are the module's own, passed through unchanged. A canceled picker answers with no files rather than a result to unwrap, and the system picker needs no permission, so none is asked for.

Install

npx expo install expo-document-picker
import { DocumentPicker } from '@ng-native/expo/document-picker';

The smallest useful example

import { Component, inject, signal } from '@angular/core';
import { Pressable, Text } from '@ng-native/components';
import { DocumentPicker } from '@ng-native/expo/document-picker';

@Component({
  selector: 'app-attach',
  imports: [Pressable, Text],
  template: `
    <pressable (press)="attach()"><text>Attach a PDF</text></pressable>
    <text>{{ attached() }}</text>
  `,
})
export class Attach {
  private readonly documents = inject(DocumentPicker);
  protected readonly attached = signal('');

  protected async attach(): Promise<void> {
    const [file] = await this.documents.pick({ type: 'application/pdf' });
    if (file) this.attached.set(file.name);
  }
}

To read what was picked, open its uri with FileSystem: await this.files.file(file.uri).text(), with files = inject(FileSystem) as a field of the component, or .bytes() for anything that is not text.

What it does

  • pick(options?) - opens the picker and resolves to the files chosen, empty if the user canceled. Each is the module's own DocumentPickerAsset: uri, name, mimeType, size and lastModified.
    • type - a MIME type such as 'image/*', or a list of them. Everything by default.
    • multiple - allow more than one file.
    • copyToCacheDirectory - on by default: each file is copied into the app's cache, so the file system and other Expo modules can read its uri. Off leaves the platform's own reference, which only some readers understand.
    • base64 - on the web, whether each uri is the file's contents as base64. On by default.

Without the module

On iOS and Android, a missing expo-document-picker - never installed, or installed without the app being rebuilt since - leaves the service made, so injecting it does not throw. A call, or a read of a value only the module knows, throws a MissingModuleError. Its message names the module and the commands that fix it; see Using a module.

On the web, and in a test that provides no fake, pick() resolves to an empty list.

Reference

DocumentPickerservice@ng-native/expo
import { DocumentPicker } from '@ng-native/expo';

Methods

pick(options?: DocumentPickerOptions): Promise<readonly PickedDocument[]>

Opens the system picker. type is a MIME type or a list of them, multiple allows more than one file, and copyToCacheDirectory (the default) copies each into the app's cache so other Expo modules can read it. Empty if they cancelled.

Angular components as native iOS and Android views, built and shipped with Expo.

An alpha. MIT licensed. Sponsor its development.

An independent project, not affiliated with or endorsed by Google, the Angular team or Expo. Angular is a trademark of Google LLC.