Langsung ke konten

Memulai

Terminal window
npm install @capgo/capacitor-downloader
npx cap sync
import { CapacitorDownloader } from '@capgo/capacitor-downloader';
// Start a download
const 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 updates
CapacitorDownloader.addListener('downloadProgress', (data) => {
console.log(`Download ${data.id}: ${data.progress}% complete`);
console.log(`Downloaded: ${data.bytesDownloaded}/${data.totalBytes} bytes`);
});
// Handle completion
CapacitorDownloader.addListener('downloadCompleted', (data) => {
console.log(`Download completed: ${data.id}`);
console.log(`File saved to: ${data.path}`);
});
// Handle errors
CapacitorDownloader.addListener('downloadFailed', (error) => {
console.error(`Download failed: ${error.id}`, error.message);
});
// Pause a download
await CapacitorDownloader.pause(downloadId);
// Resume the download
await CapacitorDownloader.resume(downloadId);
// Check download status
const status = await CapacitorDownloader.checkStatus(downloadId);
console.log('Download status:', status);
// Stop the download
await CapacitorDownloader.stop(downloadId);
  • download(options) - Mulai download file baru
  • pause(id) - Jeda download yang sedang berlangsung
  • resume(id) - Lanjutkan download yang dijeda
  • stop(id) - Hentikan dan batalkan download
  • checkStatus(id) - Dapatkan status download saat ini
  • getFileInfo(path) - Ambil informasi dan metadata file
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
}

Plugin menyediakan penanganan event komprehensif:

  • downloadProgress - Lacak progress download real-time
  • downloadCompleted - Tangani penyelesaian download sukses
  • downloadFailed - Tangani error dan kegagalan download
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 download
await CapacitorDownloader.download({
id: 'urgent-update',
url: 'https://example.com/update.zip',
destination: '/downloads/update.zip',
priority: 'high'
});

Download dapat dalam berbagai status:

  • Pending: Antri untuk download
  • Running: Sedang mendownload
  • Paused: Dihentikan sementara
  • Completed: Berhasil selesai
  • Failed: Mengalami error
  • Stopped: Dibatalkan secara manual
// Get file details
const 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);
  • Gunakan ID download unik untuk menghindari konflik
  • Implementasikan penanganan error yang tepat untuk kegagalan jaringan
  • Pertimbangkan ruang penyimpanan sebelum memulai download besar
  • Gunakan mode WiFi saja untuk file besar untuk menghemat data seluler
  • Bersihkan download yang selesai untuk mengelola penyimpanan