首页指南参考教程

Expo FileSystem iconExpo FileSystem

提供对设备上本地文件系统的访问的库。

Android
iOS
tvOS

expo-file-system 提供对设备本地存储的文件系统的访问。它还能够从网络 URL 上传和下载文件。

¥expo-file-system provides access to a file system stored locally on the device. It is also capable of uploading and downloading files from network URLs.

Diagram explaining how expo-file-system interacts with different resources
How expo-file-system works differently inside of the Expo Go app

在 Expo Go 中,每个项目都有一个单独的文件系统范围,并且无法访问其他项目的文件系统。

¥Within Expo Go, each project has a separate file system scope and has no access to the file system of other projects.

安装

¥Installation

Terminal
npx expo install expo-file-system

If you are installing this in an existing React Native app (bare workflow), start by installing expo in your project. Then, follow the additional instructions as mentioned by library's README under "Installation in bare React Native projects" section.

配置

¥Configuration

Are you using this library in a bare React Native app?

了解如何在 expo-file-system 存储库中的安装说明 文件中配置原生项目。

¥Learn how to configure the native projects in the installation instructions in the expo-file-system repository.

用法

¥Usage

下载文件

¥Downloading files

Component.js
const callback = downloadProgress => {
  const progress = downloadProgress.totalBytesWritten / downloadProgress.totalBytesExpectedToWrite;
  this.setState({
    downloadProgress: progress,
  });
};

const downloadResumable = FileSystem.createDownloadResumable(
  'http://techslides.com/demos/sample-videos/small.mp4',
  FileSystem.documentDirectory + 'small.mp4',
  {},
  callback
);

try {
  const { uri } = await downloadResumable.downloadAsync();
  console.log('Finished downloading to ', uri);
} catch (e) {
  console.error(e);
}

try {
  await downloadResumable.pauseAsync();
  console.log('Paused download operation, saving for future retrieval');
  AsyncStorage.setItem('pausedDownload', JSON.stringify(downloadResumable.savable()));
} catch (e) {
  console.error(e);
}

try {
  const { uri } = await downloadResumable.resumeAsync();
  console.log('Finished downloading to ', uri);
} catch (e) {
  console.error(e);
}

//To resume a download across app restarts, assuming the DownloadResumable.savable() object was stored:
const downloadSnapshotJson = await AsyncStorage.getItem('pausedDownload');
const downloadSnapshot = JSON.parse(downloadSnapshotJson);
const downloadResumable = new FileSystem.DownloadResumable(
  downloadSnapshot.url,
  downloadSnapshot.fileUri,
  downloadSnapshot.options,
  callback,
  downloadSnapshot.resumeData
);

try {
  const { uri } = await downloadResumable.resumeAsync();
  console.log('Finished downloading to ', uri);
} catch (e) {
  console.error(e);
}

管理 Giphy

¥Managing Giphy's

Giphy example
import * as FileSystem from 'expo-file-system';

const gifDir = FileSystem.cacheDirectory + 'giphy/';
const gifFileUri = (gifId: string) => gifDir + `gif_${gifId}_200.gif`;
const gifUrl = (gifId: string) => `https://media1.giphy.com/media/${gifId}/200.gif`;

// Checks if gif directory exists. If not, creates it
async function ensureDirExists() {
  const dirInfo = await FileSystem.getInfoAsync(gifDir);
  if (!dirInfo.exists) {
    console.log("Gif directory doesn't exist, creating…");
    await FileSystem.makeDirectoryAsync(gifDir, { intermediates: true });
  }
}

// Downloads all gifs specified as array of IDs
export async function addMultipleGifs(gifIds: string[]) {
  try {
    await ensureDirExists();

    console.log('Downloading', gifIds.length, 'gif files…');
    await Promise.all(gifIds.map(id => FileSystem.downloadAsync(gifUrl(id), gifFileUri(id))));
  } catch (e) {
    console.error("Couldn't download gif files:", e);
  }
}

// Returns URI to our local gif file
// If our gif doesn't exist locally, it downloads it
export async function getSingleGif(gifId: string) {
  await ensureDirExists();

  const fileUri = gifFileUri(gifId);
  const fileInfo = await FileSystem.getInfoAsync(fileUri);

  if (!fileInfo.exists) {
    console.log("Gif isn't cached locally. Downloading…");
    await FileSystem.downloadAsync(gifUrl(gifId), fileUri);
  }

  return fileUri;
}

// Exports shareable URI - it can be shared outside your app
export async function getGifContentUri(gifId: string) {
  return FileSystem.getContentUriAsync(await getSingleGif(gifId));
}

// Deletes whole giphy directory with all its content
export async function deleteAllGifs() {
  console.log('Deleting all GIF files…');
  await FileSystem.deleteAsync(gifDir);
}

服务器:处理多部分请求

¥Server: handling multipart requests

Node.js 中的简单服务器,可以将上传的图片保存到磁盘:

¥The simple server in Node.js, which can save uploaded images to disk:

index.js
const express = require('express');
const app = express();
const fs = require('fs');
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });

// This method will save the binary content of the request as a file.
app.patch('/binary-upload', (req, res) => {
  req.pipe(fs.createWriteStream('./uploads/image' + Date.now() + '.png'));
  res.end('OK');
});

// This method will save a "photo" field from the request as a file.
app.patch('/multipart-upload', upload.single('photo'), (req, res) => {
  // You can access other HTTP parameters. They are located in the body object.
  console.log(req.body);
  res.end('OK');
});

app.listen(3000, () => {
  console.log('Working on port 3000');
});

API

import * as FileSystem from 'expo-file-system';

目录

¥Directories

该 API 采用指向设备上本地文件的 file:// URI 来识别文件。每个应用仅对以下目录下的位置具有读写访问权限:

¥The API takes file:// URIs pointing to local files on the device to identify files. Each app only has read and write access to locations under the following directories:

因此,例如,应用用户文档目录中 'myDirectory' 下名为 'myFile' 的文件的 URI 将是 FileSystem.documentDirectory + 'myDirectory/myFile'

¥So, for example, the URI to a file named 'myFile' under 'myDirectory' in the app's user documents directory would be FileSystem.documentDirectory + 'myDirectory/myFile'.

创建文件的 Expo API 通常在这些目录中运行。这包括 Audio 录音、Camera 照片、ImagePicker 结果、SQLite 数据库和 takeSnapShotAsync() 结果。这允许它们与 FileSystem API 一起使用。

¥Expo APIs that create files generally operate within these directories. This includes Audio recordings, Camera photos, ImagePicker results, SQLite databases and takeSnapShotAsync() results. This allows their use with the FileSystem API.

某些 FileSystem 函数能够从其他位置读取(但不能写入)。

¥Some FileSystem functions are able to read from (but not write to) other locations.

SAF URI

SAF URI 是与存储访问框架兼容的 URI。它应该看起来像这个 content://com.android.externalstorage.*。获取此类 URI 的最简单方法是通过 requestDirectoryPermissionsAsync 方法。

¥A SAF URI is a URI that is compatible with the Storage Access Framework. It should look like this content://com.android.externalstorage.*. The easiest way to obtain such URI is by requestDirectoryPermissionsAsync method.

Constants

FileSystem.bundleDirectory

Type: null | string


URI to the directory where assets bundled with the application are stored.

FileSystem.cacheDirectory

Type: null | string


file:// URI pointing to the directory where temporary files used by this app will be stored. Files stored here may be automatically deleted by the system when low on storage. Example uses are for downloaded or generated files that the app just needs for one-time usage.

FileSystem.documentDirectory

Type: null | string


file:// URI pointing to the directory where user documents for this app will be stored. Files stored here will remain until explicitly deleted by the app. Ends with a trailing /. Example uses are for files the user saves that they expect to see again.

Classes

DownloadResumable

Type: Class extends FileSystemCancellableNetworkTask<DownloadProgressData>

DownloadResumable Methods

downloadAsync()

Download the contents at a remote URI to a file in the app's file system.

Returns:

Promise<undefined | FileSystemDownloadResult>

Returns a Promise that resolves to FileSystemDownloadResult object, or to undefined when task was cancelled.

fileUri()

Returns:

string

pauseAsync()

Pause the current download operation. resumeData is added to the DownloadResumable object after a successful pause operation. Returns an object that can be saved with AsyncStorage for future retrieval (the same object that is returned from calling FileSystem.DownloadResumable.savable()).

Returns a Promise that resolves to DownloadPauseState object.

resumeAsync()

Resume a paused download operation.

Returns:

Promise<undefined | FileSystemDownloadResult>

Returns a Promise that resolves to FileSystemDownloadResult object, or to undefined when task was cancelled.

savable()

Method to get the object which can be saved with AsyncStorage for future retrieval.

Returns object in shape of DownloadPauseState type.

FileSystemCancellableNetworkTask

FileSystemCancellableNetworkTask Methods

cancelAsync()

Returns:

Promise<void>

UploadTask

Type: Class extends FileSystemCancellableNetworkTask<UploadProgressData>

UploadTask Methods

Methods

FileSystem.copyAsync(options)

NameTypeDescription
optionsRelocatingOptions

A map of move options represented by RelocatingOptions type.


Create a copy of a file or directory. Directories are recursively copied with all of their contents. It can be also used to copy content shared by other apps to local filesystem.

Returns:

Promise<void>

FileSystem.createDownloadResumable(uri, fileUri, options, callback, resumeData)

NameTypeDescription
uristring

The remote URI to download from.

fileUristring

The local URI of the file to download to. If there is no file at this URI, a new one is created. If there is a file at this URI, its contents are replaced. The directory for the file must exist.

options
(optional)
DownloadOptions

A map of download options represented by DownloadOptions type.

callback
(optional)
FileSystemNetworkTaskProgressCallback<DownloadProgressData>

This function is called on each data write to update the download progress.

Note: When the app has been moved to the background, this callback won't be fired until it's moved to the foreground.

resumeData
(optional)
string

The string which allows the api to resume a paused download. This is set on the DownloadResumable object automatically when a download is paused. When initializing a new DownloadResumable this should be null.


Create a DownloadResumable object which can start, pause, and resume a download of contents at a remote URI to a file in the app's file system.

Note: You need to call downloadAsync(), on a DownloadResumable instance to initiate the download. The DownloadResumable object has a callback that provides download progress updates. Downloads can be resumed across app restarts by using AsyncStorage to store the DownloadResumable.savable() object for later retrieval. The savable object contains the arguments required to initialize a new DownloadResumable object to resume the download after an app restart. The directory for a local file uri must exist prior to calling this function.

Returns:

DownloadResumable

FileSystem.createUploadTask(url, fileUri, options, callback)

NameType
urlstring
fileUristring
options
(optional)
FileSystemUploadOptions
callback
(optional)
FileSystemNetworkTaskProgressCallback<UploadProgressData>

Returns:

UploadTask

FileSystem.deleteAsync(fileUri, options)

NameTypeDescription
fileUristring

file:// or SAF URI to the file or directory.

options
(optional)
DeletingOptions

A map of write options represented by DeletingOptions type.

Default: {}

Delete a file or directory. If the URI points to a directory, the directory and all its contents are recursively deleted.

Returns:

Promise<void>

FileSystem.deleteLegacyDocumentDirectoryAndroid()

Returns:

Promise<void>

FileSystem.downloadAsync(uri, fileUri, options)

NameTypeDescription
uristring

The remote URI to download from.

fileUristring

The local URI of the file to download to. If there is no file at this URI, a new one is created. If there is a file at this URI, its contents are replaced. The directory for the file must exist.

options
(optional)
DownloadOptions

A map of download options represented by DownloadOptions type.

Default: {}

Download the contents at a remote URI to a file in the app's file system. The directory for a local file uri must exist prior to calling this function.

Returns:

Promise<FileSystemDownloadResult>

Returns a Promise that resolves to a FileSystemDownloadResult object.

Example

FileSystem.downloadAsync(
  'http://techslides.com/demos/sample-videos/small.mp4',
  FileSystem.documentDirectory + 'small.mp4'
)
  .then(({ uri }) => {
    console.log('Finished downloading to ', uri);
  })
  .catch(error => {
    console.error(error);
  });
Only for:
Android

FileSystem.getContentUriAsync(fileUri)

NameTypeDescription
fileUristring

The local URI of the file. If there is no file at this URI, an exception will be thrown.


Takes a file:// URI and converts it into content URI (content://) so that it can be accessed by other applications outside of Expo.

Returns:

Promise<string>

Returns a Promise that resolves to a string containing a content:// URI pointing to the file. The URI is the same as the fileUri input parameter but in a different format.

Example

FileSystem.getContentUriAsync(uri).then(cUri => {
  console.log(cUri);
  IntentLauncher.startActivityAsync('android.intent.action.VIEW', {
    data: cUri,
    flags: 1,
  });
});

FileSystem.getFreeDiskStorageAsync()

Gets the available internal disk storage size, in bytes. This returns the free space on the data partition that hosts all of the internal storage for all apps on the device.

Returns:

Promise<number>

FileSystem.getInfoAsync(fileUri, options)

NameTypeDescription
fileUristring

URI to the file or directory. See supported URI schemes.

options
(optional)
InfoOptions

A map of options represented by InfoOptions type.

Default: {}

Get metadata information about a file, directory or external content/asset.

Returns:

Promise<FileInfo>

A Promise that resolves to a FileInfo object. If no item exists at this URI, the returned Promise resolves to FileInfo object in form of { exists: false, isDirectory: false }.

FileSystem.getTotalDiskCapacityAsync()

Gets total internal disk storage size, in bytes. This is the total capacity of the data partition that hosts all the internal storage for all apps on the device.

Returns:

Promise<number>

FileSystem.makeDirectoryAsync(fileUri, options)

NameTypeDescription
fileUristring

file:// URI to the new directory to create.

options
(optional)
MakeDirectoryOptions

A map of create directory options represented by MakeDirectoryOptions type.

Default: {}

Create a new empty directory.

Returns:

Promise<void>

FileSystem.moveAsync(options)

NameTypeDescription
optionsRelocatingOptions

A map of move options represented by RelocatingOptions type.


Move a file or directory to a new location.

Returns:

Promise<void>

FileSystem.readAsStringAsync(fileUri, options)

NameTypeDescription
fileUristring

file:// or SAF URI to the file or directory.

options
(optional)
ReadingOptions

A map of read options represented by ReadingOptions type.

Default: {}

Read the entire contents of a file as a string. Binary will be returned in raw format, you will need to append data:image/png;base64, to use it as Base64.

Returns:

Promise<string>

A Promise that resolves to a string containing the entire contents of the file.

FileSystem.readDirectoryAsync(fileUri)

NameTypeDescription
fileUristring

file:// URI to the directory.


Enumerate the contents of a directory.

Returns:

Promise<string[]>

A Promise that resolves to an array of strings, each containing the name of a file or directory contained in the directory at fileUri.

FileSystem.uploadAsync(url, fileUri, options)

NameTypeDescription
urlstring

The remote URL, where the file will be sent.

fileUristring

The local URI of the file to send. The file must exist.

options
(optional)
FileSystemUploadOptions

A map of download options represented by FileSystemUploadOptions type.

Default: {}

Upload the contents of the file pointed by fileUri to the remote url.

Returns:

Promise<FileSystemUploadResult>

Returns a Promise that resolves to FileSystemUploadResult object.

Example

Client

import * as FileSystem from 'expo-file-system';

try {
  const response = await FileSystem.uploadAsync(`http://192.168.0.1:1234/binary-upload`, fileUri, {
    fieldName: 'file',
    httpMethod: 'PATCH',
    uploadType: FileSystem.FileSystemUploadType.BINARY_CONTENT,
  });
  console.log(JSON.stringify(response, null, 4));
} catch (error) {
  console.log(error);
}

Server

Please refer to the "Server: Handling multipart requests" example - there is code for a simple Node.js server.

FileSystem.writeAsStringAsync(fileUri, contents, options)

NameTypeDescription
fileUristring

file:// or SAF URI to the file or directory.

Note: when you're using SAF URI the file needs to exist. You can't create a new file.

contentsstring

The string to replace the contents of the file with.

options
(optional)
WritingOptions

A map of write options represented by WritingOptions type.

Default: {}

Write the entire contents of a file as a string.

Returns:

Promise<void>

Namespaces

Only for:
Android

StorageAccessFramework

The StorageAccessFramework is a namespace inside of the expo-file-system module, which encapsulates all functions which can be used with SAF URIs. You can read more about SAF in the Android documentation.

Example

Basic Usage

import { StorageAccessFramework } from 'expo-file-system';

// Requests permissions for external directory
const permissions = await StorageAccessFramework.requestDirectoryPermissionsAsync();

if (permissions.granted) {
  // Gets SAF URI from response
  const uri = permissions.directoryUri;

  // Gets all files inside of selected directory
  const files = await StorageAccessFramework.readDirectoryAsync(uri);
  alert(`Files inside ${uri}:\n\n${JSON.stringify(files)}`);
}

Migrating an album

import * as MediaLibrary from 'expo-media-library';
import * as FileSystem from 'expo-file-system';
const { StorageAccessFramework } = FileSystem;

async function migrateAlbum(albumName: string) {
  // Gets SAF URI to the album
  const albumUri = StorageAccessFramework.getUriForDirectoryInRoot(albumName);

  // Requests permissions
  const permissions = await StorageAccessFramework.requestDirectoryPermissionsAsync(albumUri);
  if (!permissions.granted) {
    return;
  }

  const permittedUri = permissions.directoryUri;
  // Checks if users selected the correct folder
  if (!permittedUri.includes(albumName)) {
    return;
  }

  const mediaLibraryPermissions = await MediaLibrary.requestPermissionsAsync();
  if (!mediaLibraryPermissions.granted) {
    return;
  }

  // Moves files from external storage to internal storage
  await StorageAccessFramework.moveAsync({
    from: permittedUri,
    to: FileSystem.documentDirectory!,
  });

  const outputDir = FileSystem.documentDirectory! + albumName;
  const migratedFiles = await FileSystem.readDirectoryAsync(outputDir);

  // Creates assets from local files
  const [newAlbumCreator, ...assets] = await Promise.all(
    migratedFiles.map<Promise<MediaLibrary.Asset>>(
      async fileName => await MediaLibrary.createAssetAsync(outputDir + '/' + fileName)
    )
  );

  // Album was empty
  if (!newAlbumCreator) {
    return;
  }

  // Creates a new album in the scoped directory
  const newAlbum = await MediaLibrary.createAlbumAsync(albumName, newAlbumCreator, false);
  if (assets.length) {
    await MediaLibrary.addAssetsToAlbumAsync(assets, newAlbum, false);
  }
}

StorageAccessFramework Methods

copyAsync(options)

NameType
optionsRelocatingOptions

Alias for copyAsync method.

Returns:

Promise<void>

createFileAsync(parentUri, fileName, mimeType)

NameTypeDescription
parentUristring

The SAF URI to the parent directory.

fileNamestring

The name of new file without the extension.

mimeTypestring

The MIME type of new file.


Creates a new empty file.

Returns:

Promise<string>

A Promise that resolves to a SAF URI to the created file.

deleteAsync(fileUri, options)

NameType
fileUristring
options
(optional)
DeletingOptions

Alias for deleteAsync method.

Returns:

Promise<void>

getUriForDirectoryInRoot(folderName)

NameTypeDescription
folderNamestring

The name of the folder which is located in the Android root directory.


Gets a SAF URI pointing to a folder in the Android root directory. You can use this function to get URI for StorageAccessFramework.requestDirectoryPermissionsAsync() when you trying to migrate an album. In that case, the name of the album is the folder name.

Returns:

string

Returns a SAF URI to a folder.

makeDirectoryAsync(parentUri, dirName)

NameTypeDescription
parentUristring

The SAF URI to the parent directory.

dirNamestring

The name of new directory.


Creates a new empty directory.

Returns:

Promise<string>

A Promise that resolves to a SAF URI to the created directory.

moveAsync(options)

NameType
optionsRelocatingOptions

Alias for moveAsync method.

Returns:

Promise<void>

readAsStringAsync(fileUri, options)

NameType
fileUristring
options
(optional)
ReadingOptions

Alias for readAsStringAsync method.

Returns:

Promise<string>

readDirectoryAsync(dirUri)

NameTypeDescription
dirUristring

SAF URI to the directory.


Enumerate the contents of a directory.

Returns:

Promise<string[]>

A Promise that resolves to an array of strings, each containing the full SAF URI of a file or directory contained in the directory at fileUri.

Only for:
Android 11+

requestDirectoryPermissionsAsync(initialFileUrl)

NameTypeDescription
initialFileUrl
(optional)
null | string

The SAF URI of the directory that the file picker should display when it first loads. If URI is incorrect or points to a non-existing folder, it's ignored.

Default: null

Allows users to select a specific directory, granting your app access to all of the files and sub-directories within that directory.

Returns a Promise that resolves to FileSystemRequestDirectoryPermissionsResult object.

writeAsStringAsync(fileUri, contents, options)

NameType
fileUristring
contentsstring
options
(optional)
WritingOptions

Alias for writeAsStringAsync method.

Returns:

Promise<void>

Types

DeletingOptions

NameTypeDescription
idempotent
(optional)
boolean

If true, don't throw an error if there is no file or directory at this URI.

Default: false

DownloadOptions

NameTypeDescription
cache
(optional)
boolean-
headers
(optional)
Record<string, string>

An object containing all the HTTP header fields and their values for the download network request. The keys and values of the object are the header names and values respectively.

md5
(optional)
boolean

If true, include the MD5 hash of the file in the returned object. Provided for convenience since it is common to check the integrity of a file immediately after downloading.

Default: false
sessionType
(optional)
FileSystemSessionType
Only for:
iOS

A session type. Determines if tasks can be handled in the background. On Android, sessions always work in the background and you can't change it.

Default: FileSystemSessionType.BACKGROUND

DownloadPauseState

NameTypeDescription
fileUristring

The local URI of the file to download to. If there is no file at this URI, a new one is created. If there is a file at this URI, its contents are replaced.

optionsDownloadOptions

Object representing the file download options.

resumeData
(optional)
string

The string which allows the API to resume a paused download.

urlstring

The remote URI to download from.

Deprecated use FileSystemNetworkTaskProgressCallback<DownloadProgressData> instead.

DownloadProgressCallback

Type: FileSystemNetworkTaskProgressCallback<DownloadProgressData>

DownloadProgressData

NameTypeDescription
totalBytesExpectedToWritenumber

The total bytes expected to be written by the download operation. A value of -1 means that the server did not return the Content-Length header and the total size is unknown. Without this header, you won't be able to track the download progress.

totalBytesWrittennumber

The total bytes written by the download operation.

Deprecated Use FileSystemDownloadResult instead.

DownloadResult

Type: FileSystemDownloadResult

FileInfo

Type: object shaped as below:


NameTypeDescription
existstrue

Signifies that the requested file exist.

isDirectoryboolean

Boolean set to true if this is a directory and false if it is a file.

md5
(optional)
string

Present if the md5 option was truthy. Contains the MD5 hash of the file.

modificationTimenumber

The last modification time of the file expressed in seconds since epoch.

sizenumber

The size of the file in bytes. If operating on a source such as an iCloud file, only present if the size option was truthy.

uristring

A file:// URI pointing to the file. This is the same as the fileUri input parameter.


NameTypeDescription
existsfalse-
isDirectoryfalse-
uristring-

FileSystemAcceptedUploadHttpMethod

Literal Type: string

Acceptable values are: 'POST' | 'PUT' | 'PATCH'

FileSystemDownloadResult

Type: FileSystemHttpResult extended by:


NameTypeDescription
md5
(optional)
string

Present if the md5 option was truthy. Contains the MD5 hash of the file.

uristring

A file:// URI pointing to the file. This is the same as the fileUri input parameter.

FileSystemHttpResult

NameTypeDescription
headersRecord<string, string>

An object containing all the HTTP response header fields and their values for the download network request. The keys and values of the object are the header names and values respectively.

mimeTypestring | null-
statusnumber

The HTTP response status code for the download network request.

FileSystemNetworkTaskProgressCallback()

NameType
dataT

FileSystemRequestDirectoryPermissionsResult

Type: object shaped as below:


NameTypeDescription
grantedfalse-

NameTypeDescription
directoryUristring

The SAF URI to the user's selected directory. Available only if permissions were granted.

grantedtrue-

FileSystemUploadOptions

Type: UploadOptionsBinary | UploadOptionsMultipart extended by:


NameTypeDescription
headers
(optional)
Record<string, string>

An object containing all the HTTP header fields and their values for the upload network request. The keys and values of the object are the header names and values respectively.

httpMethod
(optional)
FileSystemAcceptedUploadHttpMethod

The request method.

Default: FileSystemAcceptedUploadHttpMethod.POST
sessionType
(optional)
FileSystemSessionType
Only for:
iOS

A session type. Determines if tasks can be handled in the background. On Android, sessions always work in the background and you can't change it.

Default: FileSystemSessionType.BACKGROUND

FileSystemUploadResult

Type: FileSystemHttpResult extended by:


NameTypeDescription
bodystring

The body of the server response.

InfoOptions

NameTypeDescription
md5
(optional)
boolean

Whether to return the MD5 hash of the file.

Default: false
size
(optional)
boolean

Explicitly specify that the file size should be included. For example, skipping this can prevent downloading the file if it's stored in iCloud. The size is always returned for file:// locations.

MakeDirectoryOptions

NameTypeDescription
intermediates
(optional)
boolean

If true, don't throw an error if there is no file or directory at this URI.

Default: false

ProgressEvent

NameTypeDescription
dataT-
uuidstring-

ReadingOptions

NameTypeDescription
encoding
(optional)
EncodingType | 'utf8' | 'base64'

The encoding format to use when reading the file.

Default: EncodingType.UTF8
length
(optional)
number

Optional number of bytes to read. This option is only used when encoding: FileSystem.EncodingType.Base64 and position is defined.

position
(optional)
number

Optional number of bytes to skip. This option is only used when encoding: FileSystem.EncodingType.Base64 and length is defined.

RelocatingOptions

NameTypeDescription
fromstring

URI or SAF URI to the asset, file, or directory. See supported URI schemes.

tostring

file:// URI to the file or directory which should be its new location.

UploadOptionsBinary

Upload options when upload type is set to binary.

NameTypeDescription
uploadType
(optional)
FileSystemUploadType

Upload type determines how the file will be sent to the server. Value will be FileSystemUploadType.BINARY_CONTENT.

UploadOptionsMultipart

Upload options when upload type is set to multipart.

NameTypeDescription
fieldName
(optional)
string

The name of the field which will hold uploaded file. Defaults to the file name without an extension.

mimeType
(optional)
string

The MIME type of the provided file. If not provided, the module will try to guess it based on the extension.

parameters
(optional)
Record<string, string>

Additional form properties. They will be located in the request body.

uploadTypeFileSystemUploadType

Upload type determines how the file will be sent to the server. Value will be FileSystemUploadType.MULTIPART.

UploadProgressData

NameTypeDescription
totalBytesExpectedToSendnumber

The total bytes expected to be sent by the upload operation.

totalBytesSentnumber

The total bytes sent by the upload operation.

WritingOptions

NameTypeDescription
encoding
(optional)
EncodingType | 'utf8' | 'base64'

The encoding format to use when writing the file.

Default: FileSystem.EncodingType.UTF8

Enums

EncodingType

These values can be used to define how file system data is read / written.

EncodingType Values

Base64

EncodingType.Base64 = "base64"

Binary, radix-64 representation.

UTF8

EncodingType.UTF8 = "utf8"

Standard encoding format.

Only for:
iOS

FileSystemSessionType

These values can be used to define how sessions work on iOS.

FileSystemSessionType Values

BACKGROUND

FileSystemSessionType.BACKGROUND = 0

Using this mode means that the downloading/uploading session on the native side will work even if the application is moved to background. If the task completes while the application is in background, the Promise will be either resolved immediately or (if the application execution has already been stopped) once the app is moved to foreground again.

Note: The background session doesn't fail if the server or your connection is down. Rather, it continues retrying until the task succeeds or is canceled manually.

FOREGROUND

FileSystemSessionType.FOREGROUND = 1

Using this mode means that downloading/uploading session on the native side will be terminated once the application becomes inactive (e.g. when it goes to background). Bringing the application to foreground again would trigger Promise rejection.

FileSystemUploadType

FileSystemUploadType Values

BINARY_CONTENT

FileSystemUploadType.BINARY_CONTENT = 0

The file will be sent as a request's body. The request can't contain additional data.

MULTIPART

FileSystemUploadType.MULTIPART = 1

An RFC 2387-compliant request body. The provided file will be encoded into HTTP request. This request can contain additional data represented by UploadOptionsMultipart type.

支持的 URI 方案

¥Supported URI schemes

在此表中,你可以看到每种方法可以处理什么类型的 URI。例如,如果你有一个以 content:// 开头的 URI,则不能使用 FileSystem.readAsStringAsync(),但可以使用支持此方案的 FileSystem.copyAsync()

¥In this table, you can see what type of URI can be handled by each method. For example, if you have an URI, which begins with content://, you cannot use FileSystem.readAsStringAsync(), but you can use FileSystem.copyAsync() which supports this scheme.

方法名称安卓iOS 系统
getInfoAsyncfile:///,
content://,
asset://,
无方案
file://,
ph://,
assets-library://
readAsStringAsyncfile:///,
asset://,
SAF URI
file://
writeAsStringAsyncfile:///,
SAF URI
file://
deleteAsyncfile:///,
SAF URI
file://
moveAsync来源:
file:///
SAF URI

目的地:
file://
来源:
file://

目的地:
file://
copyAsync来源:
file:///,
content://,
asset://,
SAF URI,
无方案

目的地:
file://
来源:
file://,
ph://,
assets-library://

目的地:
file://
makeDirectoryAsyncfile:///file://
readDirectoryAsyncfile:///file://
downloadAsync来源:
http://
https://

目的地:
file:///
来源:
http://
https://

目的地:
file://
uploadAsync来源:
file:///

目的地:
http://
https://
来源:
file://

目的地:
http://
https://
createDownloadResumable来源:
http://
https://

目的地:
file:///
来源:
http://
https://

目的地:
file://

在 Android 上,没有任何方案默认为打包资源。

¥On Android no scheme defaults to a bundled resource.

权限

¥Permissions

安卓

¥Android

以下权限是通过该库的 AndroidManifest.xml 自动添加的。

¥The following permissions are added automatically through this library's AndroidManifest.xml.

Android PermissionDescription

READ_EXTERNAL_STORAGE

Allows an application to read from external storage.

WRITE_EXTERNAL_STORAGE

Allows an application to write to external storage.

INTERNET

Allows applications to open network sockets.

iOS 系统

¥iOS

无需任何权限。

¥No permissions required.

Expo 中文网 - 粤ICP备13048890号