gs-web-fs
Version:
A wrapper for the browser frontend targeting the Origin Private File System (OPFS), including TypeScript type definitions and convenient file read/write functionalities that aim to be compatible with various browsers.
62 lines (43 loc) • 1.94 kB
Markdown
private file system)的封装
> 包含:在typescript中的类型定义,尽量兼容各浏览器的便捷读写文件的封装
```shell
npx yarn add gs-web-fs
```
```ts
import {openFile,readFile,saveFile} from 'https://cdn.jsdmirror.cn/npm/gs-web-fs/lib/index.web.js'
//打开图片文件
const imageFile:File = await openFile({description:'图片文件',access:'.png'})
//读取图片
const image:HTMLImageElement = await readFile(BlobTypes.Image)
//从桌面读取字符串
const {content} = await readFile(BlobTypes.Text,{description:'文本文件',access:'.txt'},'desktop')
//将文本保存到下载
await saveFile('保存.txt',content,'downloads')
//将图片文件保存到下载
await saveFile('保存.png',imageFile,'downloads')
```
```ts
import {openDirectoryHandler,readFileHandler,saveFileHandler} from 'https://cdn.jsdmirror.cn/npm/gs-web-fs/lib/index.web.js'
//从桌面打开目录
const dirHandler:FileSystemDirectoryHandle = await openDirectoryHandler('desktop')
//打开图片文件
const imageFile:FileSystemFileHandle = await openFileHandler({description:'图片文件',access:'.png'})
//读取图片
const image:HTMLImageElement = await readFileHandler(BlobTypes.Image)
//从桌面读取字符串
const {content} = await readFileHandler(BlobTypes.Text,{description:'文本文件',access:'.txt'},'desktop')
//将文本保存到下载
await saveFileHandler('保存.txt',text,'downloads')
//将图片文件保存到下载
await saveFileHandler('保存.png',await imageFile.getFile(),'downloads')
```
```typescript
/// <reference types="gs-web-fs" />
```
> 一款用于浏览器前端的针对源私有文件系统(OPFS,origin