Saving Files

Save to device keeps a file on the person's phone or computer: a ticket, a receipt, a poster. An image goes to Photos; anything else goes where the person picks; on the web, the browser downloads it. One design works on iOS, Android and the web.

Adding it

Add Save to device to a button and fill in:

FromStorage, for a file in one of your buckets, or A link, for an image field, the person’s photo, or an address that starts with https://.
Bucket, FileFor a stored file: its bucket and its path. In a list of the bucket’s files, the row’s file is chosen for you.
On successWhat runs once it is saved.
On errorWhat runs when the file cannot be fetched or written. Without it, the app says it couldn’t save the file.
On deniedWhat runs when saving to Photos is refused, as on the Asking for Permission page.
Loading & errorsOptional: a loading state of its own, for showing the download’s progress and stopping it (below). Automatic: the button is busy while the file comes.

Open a file or save it?

Open a file shows a file to look at. Save to device keeps it, so it is there later, offline.

Where it goes

An image (a JPEG, PNG, GIF, WebP or HEIC) goes to the photo library. iOS asks first, for photo saving: adding to Photos, never reading it. Its words come from App, Permission messages. Recent Android needs nothing; older Android asks to write (Flutter apps up to Android 9, Expo apps up to Android 12).

Any other file opens the system's own save dialog, and the person picks the folder. Closing it without saving runs nothing.

On the web both are downloaded, under the file's own name. The browser fetches the file from your app's address, so that address must be in Trusted Origins, as every other request from your web app needs. A link on another site that refuses it runs On error.

keep a ticket
text
Save to device   From: Storage, Bucket: tickets, File: the ticket's path
  On success     Show "Saved to your phone"
  On denied      Show "Turn on photo saving in Settings to keep it"

Sharing a file

Share can carry a file too: set its File to From storage or From a link, the same way. The file is fetched first, then the share sheet opens with the file and the text beside it. Not fetched, On error runs, or the app says it couldn't share the file.

In the Expo app the file goes alone: its share sheet takes a file or words, not both, and it doesn't say whether anything was shared, so On shared and On cancelled don't run there. Checks says so. In a browser the file is shared where the browser can share files (most phones). Where it can't, On error runs, or the app says “This browser can't share files.” A browser opens its share sheet only within a few seconds of the tap, so share a large file with Save to device on the web.

Progress and Stop

Give Save to device, or a Share with a file, its own loading state. Its progress can then fill a text (“42%”) or a progress bar as the file comes, as an upload's does. A button running Cancel a download on that loading state stops it: nothing is saved or shared, nothing that followed runs, no message shows, and the progress goes back to 0%.

a large ticket
text
Share            File: From storage, tickets, the ticket's path
                 Loading & errors: sending_ticket
Text             Value: sending_ticket's progress
Stop             Cancel a download: sending_ticket
                 Visible while sending_ticket is pending

What each export uses

Flutter. gal for Photos, file_picker's save dialog for other files, asking through permission_handler.

Expo. expo-media-library for Photos and expo-file-system for the download and the folder. The export blocks READ_MEDIA_VISUAL_USER_SELECTED, which expo-media-library declares: saving never reads the library.

Web. The browser's own download.

Sharing a file. Flutter: share_plus, the file fetched with http. Expo: expo-sharing, downloaded with expo-file-system. Web: the browser's own share sheet.

Trying it

In Preview nothing is fetched or written: it says the file would be saved, then runs On success. On a device, save an image and a PDF once each, and check Photos and the folder you picked. Share a PDF to Mail and check it arrives attached.