跳过内容

Getting Started

GitHub

您可以使用我们的 AI 助手设置来安装插件。使用以下命令将 Capgo 技能添加到您的 AI 工具中:

终端窗口
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

然后使用以下提示:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-uploader` plugin in my project.

如果您更喜欢手动设置,请按照以下命令安装插件并遵循以下平台特定的说明:

终端窗口
bun add @capgo/capacitor-uploader
bunx cap sync
import { Uploader } from '@capgo/capacitor-uploader';

开始将文件上传到服务器。

即使应用程序关闭或后台运行,上传也会继续。 监听上传事件以跟踪进度、完成或失败。

import { Uploader } from '@capgo/capacitor-uploader';
const { id } = await Uploader.startUpload({
filePath: 'file:///path/to/file.jpg',
serverUrl: 'https://example.com/upload',
headers: {
'Authorization': 'Bearer token'
},
method: 'POST',
uploadType: 'multipart',
fileField: 'photo'
});
console.log('Upload started with ID:', id);

取消并移除正在进行的上传。

这将在上传进行时停止上传并清理资源。

import { Uploader } from '@capgo/capacitor-uploader';
await Uploader.removeUpload({ id: 'upload-123' });

类型参考

类型参考

uploadOption

上传选项

上传文件时的配置选项。

export interface uploadOption {
/**
* The local file path of the file to upload.
* Can be a file:// URL or an absolute path.
*
* @since 0.0.1
*/
filePath: string;
/**
* The server URL endpoint where the file should be uploaded.
*
* @since 0.0.1
*/
serverUrl: string;
/**
* The title of the upload notification shown to the user.
* Android only.
*
* @default 'Uploading'
* @since 0.0.1
*/
notificationTitle?: string;
/**
* HTTP headers to send with the upload request.
* Useful for authentication tokens, content types, etc.
*
* @since 0.0.1
* @example
* ```typescript
* headers: {
* 'Authorization': 'Bearer token123',
* 'X-Custom-Header': 'value'
* }
* ```
*/
headers: {
[key: string]: string;
};
/**
* The HTTP method to use for the upload request.
*
* @default 'POST'
* @since 0.0.1
*/
method?: 'PUT' | 'POST';
/**
* The MIME type of the file being uploaded.
* If not specified, the plugin will attempt to determine it automatically.
*
* @since 0.0.1
* @example 'image/jpeg', 'application/pdf', 'video/mp4'
*/
mimeType?: string;
/**
* Additional form parameters to send with the upload request.
* These will be included as form data in multipart uploads.
*
* @since 0.0.1
*/
parameters?: { [key: string]: string };
/**
* The maximum number of times to retry the upload if it fails.
*
* @since 0.0.1
* @default 0
*/
maxRetries?: number;
/**
* The type of upload to perform.
* - 'binary': Uploads the file as raw binary data in the request body
* - 'multipart': Uploads the file as multipart/form-data
*
* @default 'binary'
* @since 0.0.2
*/
uploadType?: 'binary' | 'multipart';
/**
* The form field name for the file when using multipart upload type.
* Only used when uploadType is 'multipart'.
*
* @default 'file'
* @since 0.0.2
*/
fileField?: string;
}

复制到剪贴板

export interface UploadEvent {
/**
* The current status of the upload.
* - 'uploading': Upload is in progress
* - 'completed': Upload finished successfully
* - 'failed': Upload encountered an error
*
* @since 0.0.1
*/
name: 'uploading' | 'completed' | 'failed';
/**
* Additional data about the upload event.
*
* @since 0.0.1
*/
payload: {
/**
* Upload progress percentage from 0 to 100.
* Only present during 'uploading' events.
*
* @since 0.0.1
*/
percent?: number;
/**
* Error message if the upload failed.
* Only present during 'failed' events.
*
* @since 0.0.1
*/
error?: string;
/**
* HTTP status code returned by the server.
* Present during 'completed' and 'failed' events.
*
* @since 0.0.1
*/
statusCode?: number;
};
/**
* Unique identifier for this upload task.
*
* @since 0.0.1
*/
id: string;
}

真实数据来源

此页面由插件生成。

当公共__CAPGO_KEEP_0__上游更改时,重新运行同步。 src/definitions.ts. Re-run the sync when the public API changes upstream.

继续从 Getting Started

标题:继续从 Getting Started

如果您正在使用 Getting Started 来规划仪表板和 API 操作,连接它到 使用 @capgo/capacitor-uploader 为 native 能力在使用 @capgo/capacitor-uploader API 概述 为 API 概述实现细节 介绍 为介绍实现细节 API 密钥 为 API Keys 的实现细节,和 设备 为设备的实现细节,