Zum Inhalt springen

Erste Schritte

GitHub

Du kannst unser KI-gestütztes Setup nutzen, um das Plugin zu installieren. Füge die Capgo Skills mit dem folgenden Befehl deinem KI-Tool hinzu:

Terminalfenster
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

Nutze dann diesen Prompt:

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

Wenn du ein manuelles Setup bevorzugst, installiere das Plugin mit den folgenden Befehlen und befolge die plattformspezifischen Anweisungen unten:

Terminalfenster
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);
  • Berechtigungen: aufrufen checkPermissions() dann requestPermissions() bei Bedarf.
  • Listener: Handles beim Unmounten von Screens entfernen.
  • Single Source of Truth: Vertrauen src/definitions.ts im GitHub Repo.

Dateisystem anfordern.

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

Eine Datei-URL in einen Eintrag auflösen.

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

Ein Dateieintrag abrufen.

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

Ein Verzeichniseintrag abrufen.

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

Eine Datei als Text oder base64 lesen.

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

Datei als Data-URL lesen (base64 mit MIME-Typ-Präfix).

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

Daten in eine Datei schreiben.

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

Daten an eine Datei anhängen.

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

Eine Datei löschen.

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

Ein Verzeichnis erstellen.

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

Ein Verzeichnis löschen.

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

Verzeichnisinhalt lesen.

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

Metadaten einer Datei oder eines Verzeichnisses abrufen.

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

Ruft Metadaten zu einer Datei oder einem Verzeichnis ab. Alias für stat().

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

Datei oder Verzeichnis umbenennen oder verschieben.

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

Verschiebt eine Datei oder ein Verzeichnis. Alias für rename().

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

Datei oder Verzeichnis kopieren.

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

Prüfen, ob eine Datei oder ein Verzeichnis existiert.

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

Die URI einer Datei abrufen.

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

Eine Datei auf eine bestimmte Größe kürzen.

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

Alle bekannten Dateisystem-Verzeichnisse abrufen.

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

Den freien Speicherplatz in Bytes abrufen.

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

Den aktuellen Berechtigungsstatus für Dateioperationen prüfen. Auf Android werden die Berechtigungen für externen Speicher geprüft. Auf iOS und Web wird immer „granted“ zurückgegeben, da keine besonderen Berechtigungen erforderlich sind.

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

Berechtigungen für Dateioperationen anfordern. Auf Android werden die externen Speicherberechtigungen angefordert, die für den Zugriff auf Dateien außerhalb der privaten App-Verzeichnisse benötigt werden. Auf iOS und Web wird immer 'granted' zurückgegeben, da keine besonderen Berechtigungen erforderlich sind.

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

Optionen zum Anfordern eines Dateisystems.

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

Stellt ein Dateisystem dar.

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

Optionen zum Auflösen einer URL in einen Eintrag.

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

Stellt einen Datei- oder Verzeichniseintrag dar.

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

Optionen zum Abrufen einer Datei.

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

Stellt einen Dateieintrag dar.

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

Optionen zum Abrufen eines Verzeichnisses.

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

Stellt einen Verzeichniseintrag dar.

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

Optionen zum Lesen einer Datei.

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

Ergebnis des Lesens einer Datei.

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

Optionen zum Schreiben einer Datei.

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

Ergebnis des Schreibens einer Datei.

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

Diese Seite wird aus dem Plugin generiert src/definitions.ts. Führe den Sync erneut aus, wenn sich das öffentliche API upstream ändert.

Wenn du es nutzt Erste Schritte um Speicher- und Dateiverwaltung zu planen, verbinde es mit @capgo/capacitor-file verwenden für die native Funktion in „Verwendung von @capgo/capacitor-file“, @capgo/capacitor-data-storage-sqlite für die Implementierungsdetails in @capgo/capacitor-data-storage-sqlite, Mit @capgo/capacitor-data-storage-sqlite für die native Funktionalität in „@capgo/capacitor-data-storage-sqlite verwenden“, @capgo/capacitor-file für das Implementierungsdetail in @capgo/capacitor-file und @capgo/capacitor-uploader für das Implementierungsdetail in @capgo/capacitor-uploader.