본문 바로가기

시작하기

GitHub

AI-Assisted Setup으로 플러그인을 설치할 수 있습니다. 다음 명령어로 AI 도구에 Capgo 스킬을 추가하세요:

터미널 창
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';

업로드를 시작하고, 다음을 구독하세요: events, 반환된 업로드 결과를 추적합니다. id.

import { Uploader } from '@capgo/capacitor-uploader';
let uploadId = '';
const listener = await Uploader.addListener('events', async (event) => {
if (event.id !== uploadId) return;
if (event.name === 'uploading') {
console.log(event.payload.percent);
}
if (event.name === 'completed' || event.name === 'failed') {
if (event.eventId) {
await Uploader.acknowledgeEvent({ eventId: event.eventId });
}
await listener.remove();
}
});
const { id } = await Uploader.startUpload({
filePath: 'file:///data/user/0/.../scan.jpg',
serverUrl: 'https://api.example.com/v1/assets',
method: 'POST',
headers: { Authorization: 'Bearer token' },
maxRetries: 3,
});
uploadId = id;

PUT 업로드는 단일 파일만 지원합니다. presigned URL을 serverUrl 그리고 설정하세요. method: 'PUT'.

await Uploader.startUpload({
filePath: capturedUri,
serverUrl: presignedUrl,
method: 'PUT',
headers: { 'Content-Type': 'image/jpeg' },
});

예시: 추가 필드가 포함된 multipart form

“예시: 추가 필드가 포함된 multipart form” 섹션
await Uploader.uploadMultipart({
url: 'https://api.example.com/upload',
filePath: localPath,
fieldName: 'file',
fields: { userId: '42', kind: 'receipt' },
headers: { Authorization: 'Bearer token' },
});

자주 하는 실수

“흔한 실수” 섹션
  • 여러 파일: 다음을 사용하세요 files array를 startUpload 하나의 multipart 요청으로 처리합니다. PUT 여러 개의 body를 보낼 수 없습니다.
  • 웹 백그라운드: 사용자가 탭을 닫으면 업로드가 중단됩니다. 앱이 백그라운드로 전환되어도 완료되어야 한다면 native platforms를 사용하세요.
  • 취소: 호출 removeUpload({ id }) id를 사용하여 startUpload.
  • Progress: 항상 리스너를 events 시작 전에 연결하세요. 그렇지 않으면 초기 진행 상황을 놓칠 수 있습니다.

서버로 파일 업로드를 시작합니다.

앱이 종료되거나 백그라운드로 전환되어도 업로드는 계속됩니다. 업로드 이벤트를 수신해 진행 상황, 완료 또는 실패를 추적하세요.

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' });

타입 레퍼런스

“타입 참조” 섹션

파일 업로드 설정 옵션입니다.

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;
}

이 페이지는 플러그인의 src/definitions.tsupstream에서 public API가 변경되면 sync를 다시 실행하세요.

사용 중이라면 시작하기 대시보드 및 API 운영을 계획하려면, 이를 다음과 연결하세요 @capgo/capacitor-uploader 사용하기 @capgo/capacitor-uploader를 사용할 때 네이티브 기능을 위해, API 개요 구현 세부 사항은 API 개요에서 소개 구현 세부 사항은 소개에서 API 키 API Keys의 구현 세부 사항은, 기기 Devices의 구현 세부 사항은.