@ concessor/ file- transfer
File Transfer API提供了下载和上传文件的机制.
安装
npm install @capacitor/file-transfer
npx cap sync
示例
下载
import { FileTransfer } from '@capacitor/file-transfer';
import { Filesystem, Directory } from '@capacitor/filesystem';
// First get the full file path using Filesystem
const fileInfo = await Filesystem.getUri({
directory: Directory.Data,
path: 'downloaded-file.pdf'
});
try {
// Then use the FileTransfer plugin to download
await FileTransfer.downloadFile({
url: 'https://example.com/file.pdf',
path: fileInfo.uri,
progress: true
});
} catch(error) {
if (error.code === 'OS-PLUG-FLTR-0010') {
// HTTP error - see `FileTransferError` for details on fields available in `errorData`
let errorData = error.data;
this.showError('Upload failed: ' + errorData.httpStatus + '; ' + errorData.body);
} else {
// other errors - use `error.code` and `error.message` for more information.
this.showError('Upload failed: ' + error.code + '; ' + error.message);
}
}
// Progress events
FileTransfer.addListener('progress', (progress) => {
console.log(`Downloaded ${progress.bytes} of ${progress.contentLength}`);
});
上传
import { FileTransfer } from '@capacitor/file-transfer';
import { Filesystem, Directory } from '@capacitor/filesystem';
// First get the full file path using Filesystem
const fileInfo = await Filesystem.getUri({
directory: Directory.Cache,
path: 'image_upload.png'
});
try {
// Then use the FileTransfer plugin to upload
const result = await FileTransfer.uploadFile({
url: 'https://example.com/upload_api',
path: fileInfo.uri,
chunkedMode: true,
headers: {
// Upload uses `multipart/form-data` by default.
// If you want to avoid that, you can set the 'Content-Type' header explicitly.
'Content-Type': 'application/octet-stream',
},
progress: false
});
// get server response and other info from result - see `UploadFileResult` interface
} catch(error) {
if (error.code === 'OS-PLUG-FLTR-0010') {
// HTTP error - see `FileTransferError` for details on fields available in `errorData`
let errorData = error.data;
this.showError('Upload failed: ' + errorData.httpStatus + '; ' + errorData.body);
} else {
// other errors - use `error.code` and `error.message` for more information.
this.showError('Upload failed: ' + error.code + '; ' + error.message);
}
}
API 密码
注:一些输入选项来自 HttpOptions 内 @capacitor/core,但插件没有使用来自 HttpOptions兹将所使用的标准说明如下.
现有错误代码列表参见 错误。 。 。 .
下载文件(...)
downloadFile(options: DownloadFileOptions) => Promise<DownloadFileResult>
向服务器执行 HTTP 请求,并将文件下载到指定的目的地 .
如果服务器返回 HTTP 错误( 如 404, 500等), 则承诺会被拒绝 .
要获得Android和iOS运行时HTTP错误响应的信息(不适用于网络),请使用 FileTransferError 接口可用 error.data 属性.
| 参数 | 类型 |
|---|---|
options | DownloadFileOptions |
回返情况: Promise<DownloadFileResult>
自: 1.0.0 (单位:千美元)
上传文件(...)
uploadFile(options: UploadFileOptions) => Promise<UploadFileResult>
执行 HTTP 上传文件到服务器的请求 .
如果服务器返回 HTTP 错误( 如 404, 500等), 则承诺会被拒绝 .
要获得运行Android和iOS(不适用于网络)时HTTP错误响应的信息,请使用 FileTransferError 接口可用 error.data 属性.
| 参数 | 类型 |
|---|---|
options | UploadFileOptions |
回返情况: Promise<UploadFileResult>
自: 1.0.0 (单位:千美元)
添加倾听器( “ 进度 ” ,...)
addListener(eventName: "progress", listenerFunc: (progress: ProgressStatus) => void) => Promise<PluginListenerHandle>
在文件传输(下載或上傳)进度事件上添加一个收听器.
| 参数 | 类型 |
|---|---|
eventName | 'progress' |
listenerFunc | (progress: ProgressStatus) => void |
回返情况: Promise<PluginListenerHandle>
自: 1.0.0 (单位:千美元)
删除所有收听器( )
removeAllListeners() => Promise<void>
删除此插件的所有听众 .
自: 1.0.0 (单位:千美元)
接口
下载文件
| Prop | 类型 | 说明 | 自兹 |
|---|---|---|---|
path | string | 文件下载到的路径 . | 1.0.0 (单位:千美元) |
blob | Blob | 下载文件的 blob 数据 。 这只在网上提供. | 1.0.0 (单位:千美元) |
下载文件选项
| Prop | 类型 | 说明 | 自兹 |
|---|---|---|---|
url | string | 要下载文件的 URL . | 1.0.0 (单位:千美元) |
path | string | 下载文件的完整文件路径应该移动到 。 您可以使用这样的插件 @capacitor/filesystem 要获得完整的文件路径. | 1.0.0 (单位:千美元) |
progress | boolean | 如果是真的, 将会在收到的每个块上发出进度事件 。 更多信息请见添加倾听者( ) 。 在Android/iOS上,春克被压到每100米,以避免减速. 默认是 false。 。 。 . | 1.0.0 (单位:千美元) |
method | string | Http 请求方法运行 。 (过失是得到) | 1.0.0 (单位:千美元) |
params | HttpParams | 请求附加的 URL 参数 。 这个 HttpParams 界面来自 @capacitor/core。 。 。 . | 1.0.0 (单位:千美元) |
headers | HttpHeaders | Http 请求头随请求一起发送. 这个 HttpHeaders 界面来自 @capacitor/core。 。 。 . | 1.0.0 (单位:千美元) |
readTimeout | number | 需要等多久才能读取以毫秒计的额外数据. 每次收到新数据时都会重新设置 。 默认为6万毫秒(1分). 不在网络上支持 . | 1.0.0 (单位:千美元) |
connectTimeout | number | 在毫秒内等待初始连接需要多长时间. 默认为6万毫秒(1分). 在iOS中,没有真正的区分 connectTimeout财务报告和审定财务报表 readTimeout。试图使用的插件 connectTimeout,如果没有使用 readTimeout,如果不使用默认值 | 1.0.0 (单位:千美元) |
disableRedirects | boolean | 设定是否禁用 HTTP 自动方向 | 1.0.0 (单位:千美元) |
shouldEncodeUrlParams | boolean | 如果您在某些情况下需要保留未编码的 URL( 已经编码、 azure/ firebase 测试等) , 请使用此选项 。 默认是 true。在网络上不支持. | 1.0.0 (单位:千美元) |
图片
Http 信头
上传文件
| Prop | 类型 | 说明 | 自兹 |
|---|---|---|---|
bytesSent | number | 上传的字节总数 | 1.0.0 (单位:千美元) |
responseCode | string | 上传的 HTTP 响应代码 | 1.0.0 (单位:千美元) |
response | string | 上传的 HTTP 响应正文( 当可用时) | 1.0.0 (单位:千美元) |
headers | { [key: string]: string; } | 上传回复中的 HTTP 信头( 可用时) | 1.0.0 (单位:千美元) |
上传文件选项
| Prop | 类型 | 说明 | 自兹 |
|---|---|---|---|
url | string | 要上传文件的 URL . | 1.0.0 (单位:千美元) |
path | string | 要上传的文件的全部文件路径 。 您可以使用这样的插件 @capacitor/filesystem 要获得完整的文件路径. | 1.0.0 (单位:千美元) |
blob | Blob | 要上传的 Blob 数据 。 如果提供路径, 将会使用此路径 。 这只在网上提供. | 1.0.0 (单位:千美元) |
chunkedMode | boolean | 是否以块流模式上传数据 。 不在网络上支持 。 注意: 上传用途 Content-Type: multipart/form-data,当 chunkedMode 实值 true。这取决于您的后端服务器,可能导致上传失败。 如果您的服务器期望有一个生流( 例如 ) . application/octet-stream),您必须明确设置 Content-Type 页眉在 headers。 。 。 . | 1.0.0 (单位:千美元) |
mimeType | string | 要上传的数据的MIME类型. 仅在未提供"Content-Type"标题的情况下使用. | 1.0.0 (单位:千美元) |
fileKey | string | 形式元素的类型. 默认设置为"文件". 仅在未提供"Content-Type"标题的情况下使用. | 1.0.0 (单位:千美元) |
progress | boolean | 如果是真的, 将会在收到的每个块上发出进度事件 。 更多信息请见添加倾听者( ) 。 在Android/iOS上,春克被压到每100米,以避免减速. 默认是 false。 。 。 . | 1.0.0 (单位:千美元) |
method | string | Http 请求方法运行 。 (过失是 POST) | 1.0.0 (单位:千美元) |
params | HttpParams | 请求附加的 URL 参数 。 这个 HttpParams 界面来自 @capacitor/core。 。 。 . | 1.0.0 (单位:千美元) |
headers | HttpHeaders | Http 请求头随请求一起发送. 这个 HttpHeaders 界面来自 @capacitor/core。 。 。 . | 1.0.0 (单位:千美元) |
readTimeout | number | 需要等多久才能读取以毫秒计的额外数据. 每次收到新数据时都会重新设置 。 默认为6万毫秒(1分). 不在网络上支持 . | 1.0.0 (单位:千美元) |
connectTimeout | number | 在毫秒内等待初始连接需要多长时间. 默认为6万毫秒(1分). 不在网络上支持 。 在iOS中,没有真正的区分 connectTimeout财务报告和审定财务报表 readTimeout。试图使用的插件 connectTimeout,如果没有使用 readTimeout,如果不使用默认值 | 1.0.0 (单位:千美元) |
disableRedirects | boolean | 设定是否应当禁用 HTTP 自动重定向 。 不在网络上支持 . | 1.0.0 (单位:千美元) |
shouldEncodeUrlParams | boolean | 如果您在某些情况下需要保留未编码的 URL( 已经编码、 azure/ firebase 测试等) , 请使用此选项 。 默认是 true。在网络上不支持. | 1.0.0 (单位:千美元) |
插件听筒
| Prop | 类型 |
|---|---|
remove | () => Promise<void> |
进展现状
| Prop | 类型 | 说明 | 自兹 |
|---|---|---|---|
type | 'download' | 'upload' | 传输操作的类型(下載或上傳). | 1.0.0 (单位:千美元) |
url | string | 与传输相关的文件的url(下載或上傳). | 1.0.0 (单位:千美元) |
bytes | number | 至今转移的字节数 . | 1.0.0 (单位:千美元) |
contentLength | number | 与文件传输相关的字节总数 . | 1.0.0 (单位:千美元) |
lengthComputable | boolean | 内容Length值是否相关. 在某些情况下,字节的总数可能无法确定. | 1.0.0 (单位:千美元) |
文件传输错误
| Prop | 类型 | 说明 | 自兹 |
|---|---|---|---|
code | string | 识别错误的代码: OS-PLUG-FLTR---- | 1.0.0 (单位:千美元) |
message | string | 消息告知出错 | 1.0.0 (单位:千美元) |
source | string | 文件传输操作的出处( URL 为下载, 为上传的文件路径) | 1.0.0 (单位:千美元) |
target | string | 文件传输操作的目标(用于下载的文件路径,用于上传的url) | 1.0.0 (单位:千美元) |
httpStatus | number | 服务器响应的 HTTP 状态代码( 如果可用) | 1.0.0 (单位:千美元) |
headers | { [key: string]: string; } | 来自服务器响应的 HTTP 信头( 如果可用) | 1.0.0 (单位:千美元) |
body | string | 服务器的 HTTP 错误响应机构( 如果可用) | 1.0.0 (单位:千美元) |
exception | string | 向本地侧丢出例外消息( 如果有) | 1.0.0 (单位:千美元) |
错误
插件返回以下带有iOS、Android和Web特定代码的错误:
| 错误代码 | 平台( S) | 说明 |
|---|---|---|
| OS-PLUG-FLTR-0004号卫星 | Android, iOS (英语) | 该方法的输入参数无效. |
| OS-PLUG-FLTR-0005 (英语) | Android, iOS (英语) | 提供了无效的服务器 URL 或 URL 为空 . |
| OS-PLUG-FLTR-0006 (英语) | Android, iOS (英语) | 无法执行操作, 用户拒绝权限请求 . |
| OS-PLUG-FLTR-0007 (英语) | Android, iOS (英语) | 操作失败, 因为文件不存在 . |
| OS-PLUG-FLTR-0008 (英语) | Android, iOS, Web (英语) | 连接到服务器失败 . |
| OS-PLUG-FLTR-0009 (英语) | Android, iOS (英语) | 服务器响应了HTTP 304 – 没有修改. 如果您想要避免这种情况, 请检查您与 HTTP 缓存相关的信头 . |
| OS-PLUG-FLTR-0010 (英语) | Android, iOS (英语) | 服务器以HTTP错误状态代码响应. |
| OS-PLUG-FLTR-0011 (英语) | Android, iOS, Web (英语) | 操作以出错( generic error) 失败 . |
在处理应用程序中的错误时,您可以检查错误代码以确定具体的问题. 错误对象通常包含额外信息,例如:
code: 错误代码(如上表所示)message: 对错误的可读描述source: 传输源(文件路径或 URL)target: 传输的目标(文件路径或 URL)httpStatus: HTTP 状态代码(用于 HTTP 错误)body: 响应正体(用于 HTTP 错误)headers: 响应头 (用于 HTTP 错误)