首页指南参考教程

Expo DocumentPicker iconExpo DocumentPicker

一个库,提供对系统 UI 的访问,以便从用户设备上的可用提供程序中选择文档。

Android
iOS
Web

expo-document-picker 提供对系统 UI 的访问,以便从用户设备上的可用提供程序中选择文档。

¥expo-document-picker provides access to the system's UI for selecting documents from the available providers on the user's device.

安装

¥Installation

Terminal
npx expo install expo-document-picker

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.

app.json/app.config.js 中的配置

¥Configuration in app.json/app.config.js

如果你在项目中使用配置插件(EAS 构建npx expo run:[android|ios]),则可以使用其内置的 配置插件 配置 expo-document-picker。该插件允许你配置无法在运行时设置的各种属性,并且需要构建新的应用二进制文件才能生效。如果你的应用不使用 EAS Build,则你需要手动配置包。

¥You can configure expo-document-picker using its built-in config plugin if you use config plugins in your project (EAS Build or npx expo run:[android|ios]). The plugin allows you to configure various properties that cannot be set at runtime and require building a new app binary to take effect. If your app does not use EAS Build, then you'll need to manually configure the package.

Example app.json with config plugin

如果要启用 iCloud 存储功能,请按照指定的 配置属性,将 应用配置 文件中的 expo.ios.usesIcloudStorage 键设置为 true

¥If you want to enable iCloud storage features, set the expo.ios.usesIcloudStorage key to true in the app config file as specified configuration properties.

在本地运行 EAS 构建 将使用 iOS 功能签名 在构建之前启用所需的功能。

¥Running EAS Build locally will use iOS capabilities signing to enable the required capabilities before building.

app.json
{
  "expo": {
    "plugins": [
      [
        "expo-document-picker",
        {
          "iCloudContainerEnvironment": "Production"
        }
      ]
    ]
  }
}

Configurable properties

NameDefaultDescription
iCloudContainerEnvironmentundefined
Only for:
iOS

Sets the iOS com.apple.developer.icloud-container-environment entitlement used for AdHoc iOS builds. Possible values: Development, Production. Learn more.

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

不使用 EAS 构建 但需要 iCloud 存储功能 的应用必须将 手动配置支持 CloudKit 的 iCloud 服务 作为其打包包标识符。

¥Apps that don't use EAS Build and want iCloud storage features must manually configure the iCloud service with CloudKit support for their bundle identifier.

如果你通过 苹果开发者控制台 启用 iCloud 功能,请务必在 ios/[app]/[app].entitlements 文件中添加以下权利(其中 dev.expo.my-app 表示你的打包包标识符):

¥If you enable the iCloud capability through the Apple Developer Console, then be sure to add the following entitlements in your ios/[app]/[app].entitlements file (where dev.expo.my-app if your bundle identifier):

<key>com.apple.developer.icloud-container-identifiers</key>
<array>
    <string>iCloud.dev.expo.my-app</string>
</array>
<key>com.apple.developer.icloud-services</key>
<array>
    <string>CloudDocuments</string>
</array>
<key>com.apple.developer.ubiquity-container-identifiers</key>
<array>
    <string>iCloud.dev.expo.my-app</string>
</array>
<key>com.apple.developer.ubiquity-kvstore-identifier</key>
<string>$(TeamIdentifierPrefix)dev.expo.my-app</string>

Apple 开发者控制台还需要创建 iCloud 容器。注册新容器时,系统会要求你提供容器的描述和标识符。你可以在描述下输入任何名称。在标识符下,添加 iCloud.<your_bundle_identifier>(与 com.apple.developer.icloud-container-identifierscom.apple.developer.ubiquity-container-identifiers 权利使用的值相同)。

¥Apple Developer Console also requires an iCloud Container to be created. When registering the new container, you are asked to provide a description and identifier for the container. You may enter any name under the description. Under the identifier, add iCloud.<your_bundle_identifier> (same value used for com.apple.developer.icloud-container-identifiers and com.apple.developer.ubiquity-container-identifiers entitlements).

expo-file-system 一起使用

¥Using with expo-file-system

expo-document-pickerexpo-file-system 一起使用时,文件系统并不总是能够在 expo-document-picker 选择文件后立即读取该文件。

¥When using expo-document-picker with expo-file-system, it's not always possible for the file system to read the file immediately after the expo-document-picker picks it.

要允许 expo-file-system 在选择文件后立即读取文件,你需要确保 copyToCacheDirectory 选项设置为 true

¥To allow the expo-file-system to read the file immediately after it is picked, you'll need to ensure that the copyToCacheDirectory option is set to true.

API

import * as DocumentPicker from 'expo-document-picker';

Methods

DocumentPicker.getDocumentAsync(namedParameters)

NameType
namedParameters
(optional)
DocumentPickerOptions

Display the system UI for choosing a document. By default, the chosen file is copied to the app's internal cache directory.

Notes for Web: The system UI can only be shown after user activation (e.g. a Button press). Therefore, calling getDocumentAsync in componentDidMount, for example, will not work as intended. The cancel event will not be returned in the browser due to platform restrictions and inconsistencies across browsers.

Returns:

Promise<DocumentPickerResult>

On success returns a promise that fulfils with DocumentPickerResult object.

If the user cancelled the document picking, the promise resolves to { type: 'cancel' }.

Types

DocumentPickerAsset

NameTypeDescription
file
(optional)
File
Only for:
Web

File object for the parity with web File API.

lastModified
(optional)
number

Timestamp of last document modification.

mimeType
(optional)
string

Document MIME type.

namestring

Document original name.

size
(optional)
number

Document size in bytes.

uristring

An URI to the local document file.

DocumentPickerCanceledResult

Type representing canceled pick result.

NameTypeDescription
assetsnull

Always null when the request was canceled.

canceledtrue

Always true when the request was canceled.

output
(optional)
null
Only for:
Web

Always null when the request was canceled.

DocumentPickerOptions

NameTypeDescription
copyToCacheDirectory
(optional)
boolean

If true, the picked file is copied to FileSystem.CacheDirectory, which allows other Expo APIs to read the file immediately. This may impact performance for large files, so you should consider setting this to false if you expect users to pick particularly large files and your app does not need immediate read access.

Default: true
multiple
(optional)
boolean

Allows multiple files to be selected from the system UI.

Default: false
type
(optional)
string | string[]

The MIME type(s) of the documents that are available to be picked. It also supports wildcards like 'image/*' to choose any image. To allow any type of document you can use '*/*'.

Default: '*/*'

DocumentPickerResult

Literal Type: multiple types

Type representing successful and canceled document pick result.

Acceptable values are: DocumentPickerSuccessResult | DocumentPickerCanceledResult

DocumentPickerSuccessResult

Type representing successful pick result.

NameTypeDescription
assetsDocumentPickerAsset[]

An array of picked assets.

canceledfalse

If asset data have been returned this should always be false.

output
(optional)
FileList
Only for:
Web

FileList object for the parity with web File API.

Expo 中文网 - 粤ICP备13048890号