๐Ÿ“ฆ ionic-team / capacitor-file-transfer

File Transfer plugin for Capacitor โšก๏ธ

โ˜… 8 stars โ‘‚ 6 forks ๐Ÿ‘ 8 watching โš–๏ธ MIT License
๐Ÿ“ฅ Clone https://github.com/ionic-team/capacitor-file-transfer.git
HTTPS git clone https://github.com/ionic-team/capacitor-file-transfer.git
SSH git clone git@github.com:ionic-team/capacitor-file-transfer.git
CLI gh repo clone ionic-team/capacitor-file-transfer
semantic-release-bot semantic-release-bot chore(release): 2.0.3 [skip ci] 1354399 6 days ago ๐Ÿ“ History
๐Ÿ“‚ main View all commits โ†’
๐Ÿ“ .github
๐Ÿ“ packages
๐Ÿ“„ .DS_Store
๐Ÿ“„ .gitignore
๐Ÿ“„ .npmrc
๐Ÿ“„ CHANGELOG.md
๐Ÿ“„ CONTRIBUTING.md
๐Ÿ“„ LICENSE
๐Ÿ“„ package.json
๐Ÿ“„ README.md
๐Ÿ“„ README.md
Logo

@capacitor/file-transfer

The FileTransfer API provides mechanisms for downloading and uploading files.
๐Ÿ”Œ Cordova Plugin ยท ๐Ÿค– Android Library ยท ๐Ÿ iOS Library

๐Ÿ› Report Bug ยท ๐Ÿ’ก Request Feature

Install

npm install @capacitor/file-transfer
npx cap sync

Example

Download

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) {
    // handle error - see `FileTransferError` interface for what error information is returned
}

// Progress events
FileTransfer.addListener('progress', (progress) => {
  console.log(`Downloaded ${progress.bytes} of ${progress.contentLength}`);
});

Upload

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) {
    // handle error - see `FileTransferError` interface for what error information is returned
}

Documentation

Refer to this page.