z-util-page
Version:
151 lines (150 loc) • 4.42 kB
TypeScript
/**
* @module FileHelper
* @category 文件操作辅助类
*/
/**
* 文件选择
* @example
* ```ts
* choose({
* accept: [".doc",".docx","application/msword"],
* capture: "user",
* multiple: true
* }).then(files => {
* console.log(files);
* })
* .catch(err => {
* console.error(err);
* });
* ```
* @param options 文件选择配置
* @param options.accept 以逗号为分隔的[唯一文件类型说明符]列表
* @param options.capture 尝试请求使用设备的媒体捕获设备(如:摄像机),而不是请求一个文件输入。camera–照相机;camcorder–摄像机;microphone–录音
* @param options.multiple 是否允许多选
*/
export declare function choose(options?: {
accept?: Array<string>;
capture?: "user" | "environment" | "camera" | "camcorder" | "microphone";
multiple?: boolean;
}): Promise<FileList>;
/**
* H5文件下载方法
* @example
* ```ts
* save(new Blob(['你好世界'], { type: 'text/plain' }), 'test.txt');
* save('https://www.baidu.com/img/flexible/logo/pc/result@2.png', 'baidu.png');
* ```
* @param file 资源链接或者blob对象
* @param saveFileName 保存文件名
*/
export declare function save(file: string | Blob, saveFileName?: string): void;
type callback = (res: any) => void;
declare class FileReaderDecorate {
reader: FileReader;
file: File | Blob;
constructor(file: File | Blob);
abort(fun: callback): this;
error(fun: callback): this;
load(fun: callback): this;
loadstart(fun: callback): this;
loadend(fun: callback): this;
loadendPromise(): Promise<string | ArrayBuffer>;
progress(fun: callback): this;
getStatus(): 0 | 2 | 1;
getResult(): string | ArrayBuffer | null;
start(type: "ArrayBuffer" | "BinaryString" | "DataURL" | "Text"): this;
stop(): this;
}
/**
* 文件读取
* @example
* ```ts
* const reader = read(file)
* .loadend((res) => {
* console.log(res);
* })
* //start方法参数类型:"ArrayBuffer" | "BinaryString" | "DataURL" | "Text"
* .start("ArrayBuffer");
*
* //读取操作发生中断时触发
* reader.abort((abo) => {
* console.log(abo);
* })
*
* //读取操作发生错误时触发
* reader.error((err) => {
* console.log(err);
* })
*
* //读取操作完成时触发
* reader.load((res) => {
* console.log(res);
* })
*
* //读取操作开始时触发
* reader.loadstart((res) => {
* console.log(res);
* })
*
* //读取操作结束时(要么成功,要么失败)触发
* reader.loadstart((res) => {
* console.log(res);
* })
*
* //获取读取结果的promise
* const promise = reader.loadendPromise();
*
* //在读取Blob时触发。
* reader.progress((res) => {
* console.log(res);
* })
*
* //获取状态
* const status = reader.getStatus();
*
* //获取结果
* const result = reader.getResult();
*
* //中断读取
* reader.stop();
* ```
* @param file File对象或Blob对象
*/
export declare function read(file: File | Blob): FileReaderDecorate;
type FileContent = ArrayBuffer | string | Uint8Array | Blob;
/**
* 将文件写入目标文件夹
* @example
* ```ts
* //需要先调用pickDir选择文件夹
* saveFileToDir('key', 'file.txt', ['string', new Blob(['你好世界'], { type: 'text/plain' })]);
* ```
* @param dirKey 文件夹唯一标识,自行定义string,用于后续向同一文件夹写入文件
* @param fileName 文件名
* @param fileContent 二进制文件流或字符串数组
* @param overwrite 是否覆盖同名文件
* @returns { success: boolean, message: string }
*/
export declare function saveFileToDir(dirKey: string, fileName: string, fileContent: Array<FileContent | Promise<FileContent>>, overwrite?: boolean): Promise<{
success: boolean;
message: string;
}>;
/**
* 选择文件夹(与saveFileToDir共用缓存)
* @example
* ```ts
* //选择文件夹,将其与key绑定
* pickDir('key');
* //强制重新选择
* pickDir('key', true);
* ```
* @param dirKey 文件夹唯一标识,自行定义string,用于后续向同一文件夹写入文件
* @param force 是否强制重新选择
* @returns { success: boolean, message: string, data: FileSystemDirectoryHandle | null }
*/
export declare function pickDir(dirKey: string, force?: boolean): Promise<{
success: boolean;
message: string;
data: FileSystemDirectoryHandle | null;
}>;
export {};