@capacitor/file-transfer
FileTransfer API 提供了下载和上传文件的机制。
安装
npm install @capacitor/file-transfer@latest-7
npx cap sync
示例
下载
import { FileTransfer } from '@capacitor/file-transfer';
import { Filesystem, Directory } from '@capacitor/filesystem';
// 首先使用 Filesystem 获取完整的文件路径
const fileInfo = await Filesystem.getUri({
directory: Directory.Data,
path: 'downloaded-file.pdf'
});
try {
// 然后使用 FileTransfer 插件进行下载
await FileTransfer.downloadFile({
url: 'https://example.com/file.pdf',
path: fileInfo.uri,
progress: true
});
} catch(error) {
if (error.code === 'OS-PLUG-FLTR-0010') {
// HTTP 错误 - 有关 `errorData` 中可用字段的详细信息,请参阅 `FileTransferError`
let errorData = error.data;
this.showError('上传失败: ' + errorData.httpStatus + '; ' + errorData.body);
} else {
// 其他错误 - 使用 `error.code` 和 `error.message` 获取更多信息
this.showError('上传失败: ' + error.code + '; ' + error.message);
}
}
// 进度事件
FileTransfer.addListener('progress', (progress) => {
console.log(`已下载 ${progress.bytes} / ${progress.contentLength}`);
});
上传
import { FileTransfer } from '@capacitor/file-transfer';
import { Filesystem, Directory } from '@capacitor/filesystem';
// 首先使用 Filesystem 获取完整的文件路径
const fileInfo = await Filesystem.getUri({
directory: Directory.Cache,
path: 'image_upload.png'
});
try {
// 然后使用 FileTransfer 插件进行上传
const result = await FileTransfer.uploadFile({
url: 'https://example.com/upload_api',
path: fileInfo.uri,
chunkedMode: true,
headers: {
// 上传默认使用 `multipart/form-data`。
// 如果你想避免这种情况,可以显式设置 'Content-Type' 请求头。
'Content-Type': 'application/octet-stream',
},
progress: false
});
// 从结果中获取服务器响应和其他信息 - 请参阅 `UploadFileResult` 接口
} catch(error) {
if (error.code === 'OS-PLUG-FLTR-0010') {
// HTTP 错误 - 有关 `errorData` 中可用字段的详细信息,请参阅 `FileTransferError`
let errorData = error.data;
this.showError('上传失败: ' + errorData.httpStatus + '; ' + errorData.body);
} else {
// 其他错误 - 使用 `error.code` 和 `error.message` 获取更多信息
this.showError('上传失败: ' + error.code + '; ' + error.message);
}
}
API
注意:部分输入选项来自 @capacitor/core 中的 HttpOptions,但本插件并未使用 HttpOptions 中的所有参数。下面会记录实际使用的参数。
有关现有错误代码的列表,请参阅错误处理。
downloadFile(...)
downloadFile(options: DownloadFileOptions) => Promise<DownloadFileResult>
向服务器发起 HTTP 请求,并将文件下载到指定目标路径。
如果服务器返回 HTTP 错误(例如 404、500 等),Promise 将被拒绝。
在 Android 和 iOS 上运行时(不适用于 Web),要获取 HTTP 错误响应的信息,请使用 error.data 属性中可用的 FileTransferError 接口。
| 参数 | 类型 |
|---|---|
options | |
返回值:
Promise<DownloadFileResult>
自: 1.0.0
uploadFile(...)
uploadFile(options: UploadFileOptions) => Promise<UploadFileResult>
向服务器发起 HTTP 请求以上传文件。
如果服务器返回 HTTP 错误(例如 404、500 等),Promise 将被拒绝。
在 Android 和 iOS 上运行时(不适用于 Web),要获取 HTTP 错误响应的信息,请使用 error.data 属性中可用的 FileTransferError 接口。
| 参数 | 类型 |
|---|---|
options | |
返回值:
Promise<UploadFileResult>
自: 1.0.0
addListener('progress', ...)
addListener(eventName: "progress", listenerFunc: (progress: ProgressStatus) => void) => Promise<PluginListenerHandle>
为文件传输(下载或上传)进度事件添加监听器。
| 参数 | 类型 |
|---|---|
eventName | 'progress' |
listenerFunc | |
返回值:
Promise<PluginListenerHandle>
自: 1.0.0
removeAllListeners()
removeAllListeners() => Promise<void>
移除本插件的所有监听器。
自: 1.0.0
接口
DownloadFileResult
| 属性 | 类型 | 说明 | 自 |
|---|---|---|---|
path | string | 文件下载到的路径。 | 1.0.0 |
blob | Blob | 已下载文件的 Blob 数据。此属性仅在 Web 上可用。 | 1.0.0 |
| 属性 | 类型 | 说明 | 始版 |
|---|---|---|---|
url | string | 要下载文件的 URL。 | 1.0.0 |
path | string | 下载文件应移动到的完整文件路径。你可以使用像 @capacitor/filesystem 这样的插件来获取完整的文件路径。 | 1.0.0 |
progress | boolean | 如果为 true,每次接收到数据块时都会分派进度事件。更多信息请参阅 addListener()。在 Android/iOS 上,为了避免速度下降,数据块会被节流到每 100ms 一次。默认值为 false。 | 1.0.0 |
method | string | 要运行的 HTTP 请求方法。(默认为 GET) | 1.0.0 |
params | | 要附加到请求的 URL 参数。此 HttpParams 接口来自 @capacitor/core。 | 1.0.0 |
headers | | 要随请求发送的 HTTP 请求头。此 HttpHeaders 接口来自 @capacitor/core。 | 1.0.0 |
readTimeout | number | 等待读取额外数据的时长(毫秒)。每次接收到新数据时重置。默认为 60,000 毫秒(1 分钟)。Web 平台不支持。 | 1.0.0 |
connectTimeout | number | 等待初始连接的时长(毫秒)。默认为 60,000 毫秒(1 分钟)。在 iOS 上,connectTimeout 和 readTimeout 没有真正的区别。插件会尝试使用 connectTimeout,如果没有则使用 readTimeout,如果都没有则使用默认值。 | 1.0.0 |
disableRedirects | boolean | 设置是否应禁用自动 HTTP 重定向。 | 1.0.0 |
shouldEncodeUrlParams | boolean | 如果你需要在某些情况下(已经编码、Azure/Firebase 测试等)保持 URL 未编码,请使用此选项。默认值为 true。Web 平台不支持。 | 1.0.0 |