Skip to content
  • Android
  • iOS
  • Web

Document picker

Choose documents from the system picker with MIME filters and optional multiple selection.

API notes

  • Filter by MIME type and use multiple: true to select several files. Each asset includes a URI, name and last-modified timestamp, plus size and MIME type when available. Cancellation returns { canceled: true, assets: null }.
  • Native builds copy into app cache by default. With copyToCacheDirectory: false, Android returns the provider content URI and iOS returns the system-imported file URL. Release app-owned cache copies when finished.
  • Browser results include File objects and a FileList. Use base64: true when you need a data URL and release the owned blob URIs after use.

Examples

Quick start

Call from a user action on the web. Include document-picker in custom native feature profiles and rebuild the app.

nayan-ui/document-picker
import { documentPicker } from 'nayan-ui/document-picker';

export async function getPdfName() {
  const result = await documentPicker.getDocumentAsync({ type: 'application/pdf' });
  if (result.canceled) return null;
  const [pdf] = result.assets;
  if (!pdf) return null;
  try {
    return pdf.name;
  } finally {
    await documentPicker.release(pdf.uri);
  }
}

Choose several documents

Release the cached files or blob URLs after the attachments are uploaded or removed.

nayan-ui/document-picker
import { getDocumentAsync, documentPicker } from 'nayan-ui/document-picker';

export async function selectAttachments() {
  const result = await getDocumentAsync({ type: ['application/pdf', 'image/*'], multiple: true });
  if (result.canceled) return null;
  return {
    attachments: result.assets,
    dispose: () => Promise.all(result.assets.map((asset) => documentPicker.release(asset.uri)))
  };
}

Save a picked PDF to private storage

The picker uses a cache copy on native. This example replaces latest.pdf and uses the 64 MiB limit of private files.

nayan-ui/document-picker
import { assets } from 'nayan-ui/assets';
import { documentPicker } from 'nayan-ui/document-picker';
import { files } from 'nayan-ui/files';

export async function importPdf() {
  const result = await documentPicker.getDocumentAsync({ type: 'application/pdf' });
  if (result.canceled) return false;
  const [pdf] = result.assets;
  if (!pdf) return false;
  try {
    const bytes = pdf.file ? await pdf.file.arrayBuffer() : await assets.read(pdf.uri);
    const directory = await files.openDirectory('imports');
    await directory.write('latest.pdf', bytes);
    return true;
  } finally {
    await documentPicker.release(pdf.uri);
  }
}

Tags