Aller au contenu

Prise en main

GitHub

Vous pouvez utiliser notre configuration assistée par IA pour installer le plugin. Ajoutez les skills Capgo à votre outil d'IA avec la commande suivante :

Fenêtre de terminal
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

Utilisez ensuite le prompt suivant :

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

Si vous préférez une installation manuelle, installez le plugin en exécutant les commandes suivantes et suivez les instructions spécifiques à chaque plateforme ci-dessous :

Fenêtre de terminal
npm install @capgo/capacitor-file
npx cap sync
import { CapacitorFile } from '@capgo/capacitor-file';
import { CapacitorFile, FileSystemType } from '@capgo/capacitor-file';
const result = await CapacitorFile.requestFileSystem({ type: FileSystemType.TEMPORARY });
console.log(result);
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.resolveLocalFileSystemURL({
url: 'file:///data/user/0/com.example.app/files/report.pdf',
});
console.log(result);
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getFile({ path: 'path/to/file' });
console.log(result);
  • Autorisations: appelez checkPermissions() puis requestPermissions() si nécessaire.
  • Écouteurs: supprimez les handles lors du démontage des écrans.
  • Source de vérité : fiez-vous src/definitions.ts au repo GitHub.

Demander un système de fichiers.

import { CapacitorFile, FileSystemType } from '@capgo/capacitor-file';
const result = await CapacitorFile.requestFileSystem({ type: FileSystemType.TEMPORARY });
console.log(result);

Résoudre une URL de fichier vers une entrée.

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.resolveLocalFileSystemURL({ url: 'https://example.com' });
console.log(result);

Récupérer une entrée de fichier.

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getFile({ path: 'path/to/file' });
console.log(result);

Obtenir une entrée de répertoire.

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getDirectory({ path: 'path/to/file' });
console.log(result);

Lire un fichier en texte ou en base64.

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.readFile({ path: 'path/to/file' });
console.log(result);

Lire un fichier sous forme de data URL (base64 avec préfixe MIME).

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.readAsDataURL({ path: 'path/to/file' });
console.log(result);

Écrire des données dans un fichier.

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.writeFile({
path: 'path/to/file',
data: 'data',
});
console.log(result);

Ajouter des données à un fichier.

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.appendFile({
path: 'path/to/file',
data: 'data',
});
console.log(result);

Supprimer un fichier.

import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.deleteFile({ path: 'path/to/file' });

Créer un répertoire.

import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.mkdir({ path: 'path/to/file' });

Supprimer un répertoire.

import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.rmdir({ path: 'path/to/file' });

Lire le contenu d'un répertoire.

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.readdir({ path: 'path/to/file' });
console.log(result);

Récupérer les métadonnées d'un fichier ou d'un répertoire.

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.stat({ path: 'path/to/file' });
console.log(result);

Récupérer les métadonnées d'un fichier ou d'un répertoire. Alias de stat().

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getMetadata({ path: 'path/to/file' });
console.log(result);

Renommer ou déplacer un fichier ou un répertoire.

import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.rename({
from: 'from',
to: 'to',
});

Déplacez un fichier ou un répertoire. Alias de rename().

import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.move({
from: 'from',
to: 'to',
});

Copier un fichier ou un répertoire.

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.copy({
from: 'from',
to: 'to',
});
console.log(result);

Vérifier si un fichier ou un répertoire existe.

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.exists({ path: 'path/to/file' });
console.log(result);

Récupérer l'URI d'un fichier.

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getUri({ path: 'path/to/file' });
console.log(result);

Tronquer un fichier à une taille spécifiée.

import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.truncate({ path: 'path/to/file' });

Get all known file system directories.

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getDirectories();
console.log(result);

Obtenir l'espace disque libre en octets.

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getFreeDiskSpace();
console.log(result);

Vérifiez l'état actuel des autorisations pour les opérations sur les fichiers. Sur Android, cela vérifie les autorisations de stockage externe. Sur iOS et le web, cela renvoie toujours « granted », car aucune autorisation spéciale n'est requise.

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.checkPermissions();
console.log(result);

Demander les permissions pour les opérations de fichiers. Sur Android, cela demande les permissions de stockage externe nécessaires pour accéder aux fichiers en dehors des répertoires privés de l'app. Sur iOS et le web, cela renvoie toujours « granted », aucune permission spéciale n'étant requise.

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.requestPermissions();
console.log(result);

Options pour demander l'accès à un système de fichiers.

export interface RequestFileSystemOptions {
/** The type of file system to request */
type: FileSystemType;
/** Requested size in bytes (may not be enforced on all platforms) */
size?: number;
}

Représente un système de fichiers.

export interface FileSystem {
/** The name of the file system */
name: string;
/** The root directory of the file system */
root: DirectoryEntry;
}

Options pour résoudre une URL en une entrée.

export interface ResolveURLOptions {
/** The URL to resolve (file:// or cdvfile://) */
url: string;
}

Représente une entrée de fichier ou de répertoire.

export interface Entry {
/** True if this is a file */
isFile: boolean;
/** True if this is a directory */
isDirectory: boolean;
/** The name of the file or directory */
name: string;
/** The full path relative to the filesystem root */
fullPath: string;
/** The native file:// URI */
nativeURL: string;
}

Options pour récupérer un fichier.

export interface GetFileOptions {
/** Path to the file */
path: string;
/** Base directory */
directory?: Directory;
/** Options for creating the file */
options?: GetOptions;
}

Représente une entrée de fichier.

export interface FileEntry extends Entry {
isFile: true;
isDirectory: false;
}

Options pour obtenir un répertoire.

export interface GetDirectoryOptions {
/** Path to the directory */
path: string;
/** Base directory */
directory?: Directory;
/** Options for creating the directory */
options?: GetOptions;
}

Représente une entrée de répertoire.

export interface DirectoryEntry extends Entry {
isFile: false;
isDirectory: true;
}

Options pour lire un fichier.

export interface ReadFileOptions {
/** Path to the file */
path: string;
/** Base directory */
directory?: Directory;
/** Encoding for text files (omit for binary/base64) */
encoding?: Encoding;
/** Byte offset to start reading from (default: 0) */
offset?: number;
/** Number of bytes to read (default: read to end of file) */
length?: number;
}

Résultat de la lecture d'un fichier.

export interface ReadFileResult {
/** File contents as string (text) or base64 (binary) */
data: string;
}

Options pour écrire un fichier.

export interface WriteFileOptions {
/** Path to the file */
path: string;
/** Base directory */
directory?: Directory;
/** Data to write (string for text, base64 for binary) */
data: string;
/** Encoding for text files */
encoding?: Encoding;
/** If true, append to existing file instead of overwriting */
append?: boolean;
/** Create intermediate directories if they don't exist */
recursive?: boolean;
/** Byte position to start writing at (for random access writes). If not specified, writes from beginning or appends based on 'append' flag */
position?: number;
}

Résultat de l’écriture d’un fichier.

export interface WriteFileResult {
/** The URI of the written file */
uri: string;
}

Cette page est générée à partir de src/definitions.ts. Relancez la synchronisation lorsque l’API public change en amont.

Si vous utilisez Prise en main pour planifier le stockage et la gestion des fichiers, connectez-l’à Utiliser @capgo/capacitor-file pour la fonctionnalité native de « Utiliser @capgo/capacitor-file », @capgo/capacitor-data-storage-sqlite pour les détails d'implémentation dans @capgo/capacitor-data-storage-sqlite, Utiliser @capgo/capacitor-data-storage-sqlite pour la capacité native dans Utiliser @capgo/capacitor-data-storage-sqlite @capgo/capacitor-file pour le détail d'implémentation dans @capgo/capacitor-file, et @capgo/capacitor-uploader pour les détails d'implémentation dans @capgo/capacitor-uploader.