LynxFilePicker
Lets someone pick documents from their phone, such as a PDF, a spreadsheet or a CSV, and save a file your app made where they choose. import { LynxFilePicker, FilePickerError, FILE_PICKER_MAX_BYTES, type FilePickerOptions, type FilePickerErrorCode, type PickedFile, type SaveFile } from '@blotch/angular-lynx';
Lynx has no <input type="file"> and no downloads, and LynxImagePicker only offers photos. Use this for an expense receipt, a CV or a contract to upload, or an invoice or report to export.
Turn it on
Add filePicker to the build target's options in angular.json:
What it adds:
- iOS: the module that opens
UIDocumentPickerViewController. - Android: the module, and the lines that launch the system's document picker and save screen.
Neither platform asks the person for a permission, so there's no reason to give. They choose each file or place, and the app reads or writes only that.
Without it, the native code behind the picker is left out of both apps, and pick() and save() reject with unavailable.
For a folder of its own in the iOS Files app, under On My iPhone, also set "fileSharing": true. The person can then save a file there and pick it again later. It adds UIFileSharingEnabled and LSSupportsOpeningDocumentsInPlace to the iOS app. Android has no such folder: its picker already shows Downloads.
See Turn on device features for every setting.
Usage
Expenses stands for the app's own service.
API
LynxFilePicker
LynxFilePicker.pick()
Opens the picker and resolves with the files picked, or with [] when the person closes it without choosing. Rejects with a FilePickerError, or with a TypeError for a type that isn't a MIME type or an extension.
One picker can be open at a time. Picking again while one is open:
- On iOS and Android, rejects with
failed. - In the web preview, resolves the open pick with
[].
LynxFilePicker.recover()
Resolves with the files the person picked while Android restarted the app, which no pick() was left to resolve with, or with [] when there are none. Rejects with a FilePickerError with too-large or failed when they couldn't be read, as pick() does.
Android can end the app's screen while the picker is in front, when it needs the memory or with "Don't keep activities" on, and start it again when the person has chosen. The app starts again with it, so the pick() that opened the picker has gone. Call recover() as the app starts:
- It resolves with the files once, read with the same 20 MiB limit as the lost
pick(). - It waits while Android still has to hand the restarted app the result, until the picker closes.
- It reads the files only when it's called, so an app that never calls it doesn't read them.
- Files nobody asks for aren't handed over after a minute, and are dropped when another picker or save screen opens, even if the person cancels it, or when the person closes the app or the app ends.
- A
save()lost the same way needs no call: the Android app writes the file where the person chose as soon as they choose. Nothing tells the restarted app, since itssave()has gone.
FilePickerOptions
types takes MIME types (application/pdf), a family (image/*) or extensions (.docx).
PickedFile
LynxFilePicker.save()
Lets the person choose where to save the file, under name, which they can change. The Blob's type tells the platform what kind of file it is.
Resolves with true once it's written, or false when they close the screen without saving. Rejects with a FilePickerError, or with a TypeError without a Blob and a name.
FILE_PICKER_MAX_BYTES
The most bytes one pick, all its files together, or one save carries: 20 MiB. The bytes cross between the platform and your app as text, which phones with little memory can hold at this size. A larger file rejects with too-large, before it's read.
FilePickerError
Testing
The testing library stands in for the person. Until a spec calls setPickedFiles(), they close the picker; savedFiles() lists what the app saved:
Under the testing library's fake module, recover() resolves with []. A stand-in for LynxFilePicker in a component that calls it needs it too, such as recover: async () => [].
Related
- Turn on device features: the
filePickerandfileSharingoptions and the other opt-in build options LynxImagePicker: pick a photo, scaled downLynxShare: send a file to another app, or save a photo
