Skip to content

LPegasus/filesystem-api-explore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

[TOC]

FileSystem APIs

接口类型

DOMFileSystem

root : DirectoryEntry

根目录

name : String

名称

DirectoryEntry & FileEntry inherit from Entry

  • properties

    • filesystem : DOMFileSystem

      指向 DOMFileSystem

    • fullPath : String

      绝对路径

    • isDirectory : Boolean

      是否是 folder

    • isFile : Boolean

      是否是 file

    • name : String

      文件名

  • methods

    • createWriter: (callback: (writer: FileWriter) => void) => void (FileEntry Only)

      fileEntry.createWriter(function(fileWriter) {
        fileWriter.onwriteend = function(e) {
          // success todo...
        }
      
        fileWriter.onerror = function(e) {
          // fail todo...
        }
        
        fileWriter.write(new Blob(['...'],
        { type: 'text/plain;charset=UTF-8', endings: 'native' /* 'transparet' */ }
        ));
      
      }, errorHandler);
    • file: (callback: (file: File) => void) => void (FileEntry Only)

      fileEntry.file(function(file: File) {
        const reader = new FileReader();
        reader.onloadend = function (e) {
      	// success todo...
        }
      
        reader.onerror = function (e) {
          // fail todo...
        }
      
        reader.readAsText(file);
      }, errorHandler);
    • copyTo: (dirEntry: DirectoryEntry, newName?: String, successCallback?, errorCallback?) => void

      复制

      root.getFile(filename, {}, function(fileEntry: FileEntry) {// get fileEntry
        root.getDirectory(dest, {}, function(dirEntry: DirectoryEntry) {// get dest dirEntry
          fileEntry.copyTo(dirEntry, newName, onsuccess, onerror));
        });
      });
    • moveTo: (dest: DirectoryEntry, newName?: String, successCallback?: (newEntry: FileEntry) => void, errorCallback?: (err) => void) => void

      剪切

      function rename(cwd: DirectoryEntry, src: String, newName: String) {
        cwd.getFile(src, {}, function(fileEntry) {
          fileEntry.moveTo(cwd, newName);
        }, errorHandler);
      }
    • createReader: DirectoryReader (DirectoryEntry Only)

      读文件夹

      const reader = root.createReader();
      reader.readEntries(function(results: Array<Entry>) {
        if (!results.length) {
          // is empty folder
        } else {
          const fileEntryList = results.filter(
            function(entry) { return entry.isFile; }
          );
      
          const directoryEntryList = results.filter(
            function(entry) { return entry.isDirectory; }
          );
        }
      });
    • getFile & getDirectory: (pathname: String, opts:{create?: Boolean, exclusive?: Boolean}, successCallback?: (fileEntry: FileEntry) => void, errorCallback?) => void (DirectoryEntry Only)

      创建、获取文件

      fs.root.getFile(src, {}, function(fileEntry: FileEntry) {
      
        fs.root.getDirectory(dirName, {}, function(dirEntry: DirectoryEntry) {
          fileEntry.moveTo(dirEntry);
        }, errorHandler);
      
      }, errorHandler);
    • toURL: String

      const img = new Image(fileEntry.toURL());
      document.body.append(img);
    • remove: (successCallback?: () => void, errorCallback?: (err: Error) => void) => void

      fileEntry.remove(function() {
        // removed successfully...
      }, function(err) {
        // failed...
      });
    • getMetaData: (callback: (metadata: Metadata) => void) => void

      获取文件信息(最后变更时间、大小)

      fs.root.getFile('folderA/folderB/file.jpg', {}, function(fileEntry: FileEntry) {
        fileEntry.getMetaData(function(metadata: Metadata) {
          console.log(metadata);
        });
      }, errorHandler);

About

WebAPI FileSystem APIs demo

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published