入门指南
Copy a setup prompt with the install steps and the full markdown guide for this plugin.
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-downloader`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/zh/docs/plugins/downloader/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
npm install @capgo/capacitor-downloadernpx cap syncyarn add @capgo/capacitor-downloadernpx cap syncpnpm add @capgo/capacitor-downloadernpx cap syncbun add @capgo/capacitor-downloadernpx cap syncimport { CapacitorDownloader } from '@capgo/capacitor-downloader';
// Start a downloadconst downloadId = 'my-download-001';await CapacitorDownloader.download({ id: downloadId, url: 'https://example.com/large-file.zip', destination: '/downloads/large-file.zip', headers: { 'Authorization': 'Bearer token123' }, network: 'wifi-only', priority: 'high'});
// Listen for progress updatesCapacitorDownloader.addListener('downloadProgress', (data) => { console.log(`Download ${data.id}: ${data.progress}% complete`); console.log(`Downloaded: ${data.bytesDownloaded}/${data.totalBytes} bytes`);});
// Handle completionCapacitorDownloader.addListener('downloadCompleted', (data) => { console.log(`Download completed: ${data.id}`); console.log(`File saved to: ${data.path}`);});
// Handle errorsCapacitorDownloader.addListener('downloadFailed', (error) => { console.error(`Download failed: ${error.id}`, error.message);});
// Pause a downloadawait CapacitorDownloader.pause(downloadId);
// Resume the downloadawait CapacitorDownloader.resume(downloadId);
// Check download statusconst status = await CapacitorDownloader.checkStatus(downloadId);console.log('Download status:', status);
// Stop the downloadawait CapacitorDownloader.stop(downloadId);核心 API 方法
Section titled “核心 API 方法”download(options)- 开始新的文件下载pause(id)- 暂停正在进行的下载resume(id)- 恢复暂停的下载stop(id)- 停止并取消下载checkStatus(id)- 获取当前下载状态
getFileInfo(path)- 检索文件信息和元数据
interface DownloadOptions { id: string; // Unique download identifier url: string; // Download source URL destination: string; // Local save path headers?: Record<string, string>; // Custom HTTP headers network?: 'cellular' | 'wifi-only'; // Network restrictions priority?: 'high' | 'normal' | 'low'; // Download priority}插件提供全面的事件处理:
downloadProgress- 跟踪实时下载进度downloadCompleted- 处理成功的下载完成downloadFailed- 处理下载错误和失败
仅 WiFi 下载
Section titled “仅 WiFi 下载”await CapacitorDownloader.download({ id: 'large-file', url: 'https://example.com/video.mp4', destination: '/downloads/video.mp4', network: 'wifi-only' // Restricts to WiFi networks only});// High priority downloadawait CapacitorDownloader.download({ id: 'urgent-update', url: 'https://example.com/update.zip', destination: '/downloads/update.zip', priority: 'high'});下载可以处于各种状态:
- Pending: 排队等待下载
- Running: 正在下载
- Paused: 暂时停止
- Completed: 成功完成
- Failed: 遇到错误
- Stopped: 手动取消
// Get file detailsconst fileInfo = await CapacitorDownloader.getFileInfo('/downloads/my-file.pdf');console.log('File size:', fileInfo.size);console.log('Last modified:', fileInfo.lastModified);console.log('MIME type:', fileInfo.mimeType);- 使用唯一的下载 ID 以避免冲突
- 实施适当的错误处理以应对网络故障
- 在开始大型下载前考虑存储空间
- 对大型文件使用仅 WiFi 模式以节省移动数据
- 清理已完成的下载以管理存储空间