Comprehensive local storage operations and file system management capabilities.
Store and retrieve data locally with both synchronous and asynchronous APIs.
/**
* Set storage data asynchronously
* @param options - Storage options
*/
function setStorage(options: SetStorageOptions): void;
/**
* Set storage data synchronously
* @param key - Storage key
* @param data - Data to store
*/
function setStorageSync(key: string, data: any): void;
/**
* Get storage data asynchronously
* @param options - Get storage options
*/
function getStorage(options: GetStorageOptions): void;
/**
* Get storage data synchronously
* @param key - Storage key
* @returns Stored data
*/
function getStorageSync(key: string): any;
/**
* Get storage information asynchronously
* @param options - Options with success/fail callbacks
*/
function getStorageInfo(options: GetStorageInfoOptions): void;
/**
* Get storage information synchronously
* @returns Storage information
*/
function getStorageInfoSync(): StorageInfo;
/**
* Remove storage item asynchronously
* @param options - Remove storage options
*/
function removeStorage(options: RemoveStorageOptions): void;
/**
* Remove storage item synchronously
* @param key - Storage key to remove
*/
function removeStorageSync(key: string): void;
/**
* Clear all storage asynchronously
* @param options - Options with success/fail callbacks
*/
function clearStorage(options?: ClearStorageOptions): void;
/**
* Clear all storage synchronously
*/
function clearStorageSync(): void;
interface SetStorageOptions {
key: string;
data: any;
success?: (result: any) => void;
fail?: (error: any) => void;
complete?: () => void;
}
interface GetStorageOptions {
key: string;
success?: (result: GetStorageResult) => void;
fail?: (error: any) => void;
complete?: () => void;
}
interface GetStorageResult {
data: any;
}
interface StorageInfo {
keys: string[];
currentSize: number;
limitSize: number;
}Usage Examples:
import uni from "@dcloudio/uni-app-plus";
// Async storage operations
uni.setStorage({
key: 'user_profile',
data: {
name: 'John Doe',
email: 'john@example.com',
preferences: { theme: 'dark' }
},
success: () => {
console.log('Profile saved');
}
});
uni.getStorage({
key: 'user_profile',
success: (res) => {
console.log('User profile:', res.data);
},
fail: (error) => {
console.error('Failed to get profile:', error);
}
});
// Sync storage operations
try {
uni.setStorageSync('app_settings', {
language: 'en',
notifications: true
});
const settings = uni.getStorageSync('app_settings');
console.log('Settings:', settings);
} catch (error) {
console.error('Storage operation failed:', error);
}
// Get storage info
uni.getStorageInfo({
success: (res) => {
console.log('Storage keys:', res.keys);
console.log('Used space:', res.currentSize + 'KB');
console.log('Total space:', res.limitSize + 'KB');
}
});
// Remove specific item
uni.removeStorage({
key: 'temp_data',
success: () => {
console.log('Temp data removed');
}
});
// Clear all storage
uni.clearStorage({
success: () => {
console.log('All storage cleared');
}
});Save, retrieve, and manage files on the device.
/**
* Save file to local storage
* @param options - Save file options
*/
function saveFile(options: SaveFileOptions): void;
/**
* Get list of saved files
* @param options - Options with success/fail callbacks
*/
function getSavedFileList(options?: GetSavedFileListOptions): void;
/**
* Get information about a saved file
* @param options - Get file info options
*/
function getSavedFileInfo(options: GetSavedFileInfoOptions): void;
/**
* Remove a saved file
* @param options - Remove file options
*/
function removeSavedFile(options: RemoveSavedFileOptions): void;
/**
* Get file information
* @param options - Get file info options
*/
function getFileInfo(options: GetFileInfoOptions): void;
interface SaveFileOptions {
tempFilePath: string;
filePath?: string;
success?: (result: SaveFileResult) => void;
fail?: (error: any) => void;
complete?: () => void;
}
interface SaveFileResult {
savedFilePath: string;
}
interface GetSavedFileListOptions {
success?: (result: SavedFileListResult) => void;
fail?: (error: any) => void;
complete?: () => void;
}
interface SavedFileListResult {
fileList: SavedFileInfo[];
}
interface SavedFileInfo {
filePath: string;
createTime: number;
size: number;
}
interface GetSavedFileInfoOptions {
filePath: string;
success?: (result: SavedFileInfo) => void;
fail?: (error: any) => void;
complete?: () => void;
}
interface GetFileInfoOptions {
filePath: string;
digestAlgorithm?: 'md5' | 'sha1';
success?: (result: FileInfoResult) => void;
fail?: (error: any) => void;
complete?: () => void;
}
interface FileInfoResult {
size: number;
digest?: string;
errMsg: string;
}Open and handle various document types.
/**
* Open document with system default app
* @param options - Open document options
*/
function openDocument(options: OpenDocumentOptions): void;
interface OpenDocumentOptions {
filePath: string;
fileType?: string;
showMenu?: boolean;
success?: (result: any) => void;
fail?: (error: any) => void;
complete?: () => void;
}Usage Example:
// Open PDF document
uni.openDocument({
filePath: '/path/to/document.pdf',
fileType: 'pdf',
showMenu: true,
success: () => {
console.log('Document opened successfully');
},
fail: (error) => {
console.error('Failed to open document:', error);
}
});Advanced file system operations through the FileSystemManager.
/**
* Get file system manager instance
* @returns FileSystemManager instance
*/
function getFileSystemManager(): FileSystemManager;
interface FileSystemManager {
readFile(options: ReadFileOptions): void;
readFileSync(filePath: string, encoding?: string, position?: number, length?: number): string | ArrayBuffer;
writeFile(options: WriteFileOptions): void;
writeFileSync(filePath: string, data: string | ArrayBuffer, encoding?: string): void;
appendFile(options: AppendFileOptions): void;
appendFileSync(filePath: string, data: string | ArrayBuffer, encoding?: string): void;
copyFile(options: CopyFileOptions): void;
copyFileSync(srcPath: string, destPath: string): void;
getFileInfo(options: GetFileInfoOptions): void;
getFileInfoSync(filePath: string): FileStats;
mkdir(options: MkdirOptions): void;
mkdirSync(dirPath: string, recursive?: boolean): void;
readdir(options: ReaddirOptions): void;
readdirSync(dirPath: string): string[];
removedir(options: RemoveDirOptions): void;
removedirSync(dirPath: string): void;
rename(options: RenameOptions): void;
renameSync(oldPath: string, newPath: string): void;
rmdir(options: RmdirOptions): void;
rmdirSync(dirPath: string): void;
stat(options: StatOptions): void;
statSync(path: string): FileStats;
unlink(options: UnlinkOptions): void;
unlinkSync(filePath: string): void;
unzip(options: UnzipOptions): void;
access(options: AccessOptions): void;
accessSync(path: string): void;
}
interface ReadFileOptions {
filePath: string;
encoding?: 'ascii' | 'base64' | 'binary' | 'hex' | 'ucs2' | 'ucs-2' | 'utf16le' | 'utf-16le' | 'utf8' | 'utf-8' | 'latin1';
position?: number;
length?: number;
success?: (result: ReadFileResult) => void;
fail?: (error: any) => void;
complete?: () => void;
}
interface ReadFileResult {
data: string | ArrayBuffer;
}
interface WriteFileOptions {
filePath: string;
data: string | ArrayBuffer;
encoding?: string;
success?: (result: any) => void;
fail?: (error: any) => void;
complete?: () => void;
}
interface FileStats {
mode: number;
size: number;
lastAccessedTime: number;
lastModifiedTime: number;
isDirectory(): boolean;
isFile(): boolean;
}Usage Examples:
const fs = uni.getFileSystemManager();
// Read file
fs.readFile({
filePath: '/path/to/file.txt',
encoding: 'utf8',
success: (res) => {
console.log('File content:', res.data);
},
fail: (error) => {
console.error('Failed to read file:', error);
}
});
// Write file
fs.writeFile({
filePath: '/path/to/output.txt',
data: 'Hello, World!',
encoding: 'utf8',
success: () => {
console.log('File written successfully');
}
});
// Create directory
fs.mkdir({
dirPath: '/path/to/new/directory',
recursive: true,
success: () => {
console.log('Directory created');
}
});
// List directory contents
fs.readdir({
dirPath: '/path/to/directory',
success: (res) => {
console.log('Directory contents:', res.files);
}
});
// Get file stats
fs.stat({
path: '/path/to/file',
success: (res) => {
console.log('File size:', res.stats.size);
console.log('Is directory:', res.stats.isDirectory());
}
});
// Copy file
fs.copyFile({
srcPath: '/path/to/source.txt',
destPath: '/path/to/destination.txt',
success: () => {
console.log('File copied successfully');
}
});interface GetStorageInfoOptions {
success?: (result: StorageInfo) => void;
fail?: (error: any) => void;
complete?: () => void;
}
interface RemoveStorageOptions {
key: string;
success?: (result: any) => void;
fail?: (error: any) => void;
complete?: () => void;
}
interface ClearStorageOptions {
success?: (result: any) => void;
fail?: (error: any) => void;
complete?: () => void;
}interface AppendFileOptions {
filePath: string;
data: string | ArrayBuffer;
encoding?: string;
success?: (result: any) => void;
fail?: (error: any) => void;
complete?: () => void;
}
interface CopyFileOptions {
srcPath: string;
destPath: string;
success?: (result: any) => void;
fail?: (error: any) => void;
complete?: () => void;
}
interface MkdirOptions {
dirPath: string;
recursive?: boolean;
success?: (result: any) => void;
fail?: (error: any) => void;
complete?: () => void;
}
interface ReaddirOptions {
dirPath: string;
success?: (result: ReaddirResult) => void;
fail?: (error: any) => void;
complete?: () => void;
}
interface ReaddirResult {
files: string[];
}
interface StatOptions {
path: string;
recursive?: boolean;
success?: (result: StatResult) => void;
fail?: (error: any) => void;
complete?: () => void;
}
interface StatResult {
stats: FileStats;
}
interface UnlinkOptions {
filePath: string;
success?: (result: any) => void;
fail?: (error: any) => void;
complete?: () => void;
}
interface AccessOptions {
path: string;
success?: (result: any) => void;
fail?: (error: any) => void;
complete?: () => void;
}