首页 > 网站 > WEB开发 > 正文

HTML5文件系统API和资料整理

2024-04-27 14:10:37
字体:大 中 小
来源:转载
供稿:网友

HTML5文件系统API和资料整理

  来着火狐开发网络的官方文档:点我打开;

  W3C的官方文档: 点我打开;

  园友的博客: 点我打开;

  浏览器兼容性, 好了就Chrome支持, 我刚刚更新的火狐37也不支持, nice, 太nice了:

  如果我们在http://localhost/下使用文件系统api创建了虚拟文件, 那么通过一下地址可以访问文件系统:filesystem:http://localhos1/persistent/;

  如果没有文件系统没有本地文件, 那么应该是一个错误界面:

 

  就chrome支持本地文件系统, 所以兼容完全不用管那么多了, 我们先通过 requestFileSystem获取文件句柄, 第一个参数为临时文件系统,还是永久文件系统; 第二个参数为请求的空间大小; 第三个是回调函数; 

            //window.TEMPORARY 是 0 , window.PERSISTENT是1;            storeType = storeType === 0 ? window.TEMPORARY : window.PERSISTENT;            window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;            window.requestFileSystem(storeType, 5 * 1024 * 1024, function (fs) {                console.log(fs);//文件系统根目录句柄(root);            }, errorHandler);        

  上面回调函数会返回一个参数fs,这个参数“fs”很重要, fs下面有很多方法, 主要是操作文件或者目录:

  getDirectory是在对象上面的方法, 这个主要是处理目录,比如打开目录, 或者新建目录:

ParameterTypeNullableOptionalDescription
pathDOMString??Either an absolute path or a relative path from this DirectoryEntry to the directory to be looked up or created. It is an error to attempt to create a directory whose immediate parent does not yet exist.
optionsFlags??
  • If create and exclusive are both true and the path already exists, getDirectory MUST fail.
  • If create is true, the path doesn't exist, and no other error occurs, getDirectory MUST create and return a corresponding DirectoryEntry.
  • If create is not true and the path doesn't exist, getDirectory MUST fail.
  • If create is not true and the path exists, but is a file, getDirectory MUST fail.
  • Otherwise, if no other error occurs, getDirectory MUST return a DirectoryEntry corresponding to path.
successCallbackEntryCallback??A callback that is called to return the DirectoryEntry selected or created.
errorCallbackErrorCallback??A callback that is called when errors happen.

  

  getFile这个是获取文件, 作用是打开文件或者新建文件;

ParameterTypeNullableOptionalDescription
pathDOMString??Either an absolute path or a relative path from this DirectoryEntry to the file to be looked up or created. It is an error to attempt to create a file whose immediate parent does not yet exist.
optionsFlags??
  • If create and exclusive are both true, and the path already exists, getFile MUST fail.
  • If create is true, the path doesn't exist, and no other error occurs, getFile MUST create it as a zero-length file and return a corresponding FileEntry.
  • If create is not true and the path doesn't exist, getFile MUST fail.
  • If create is not true and the path exists, but is a directory, getFile MUST fail.
  • Otherwise, if no other error occurs, getFile MUST return a FileEntry corresponding to path.
successCallbackEntryCallback??A callback that is called to return the File selected or created.
errorCallbackErrorCallback??A callback that is called when errors happen.

    通过上面的方法,我们就获取到了文件句柄啦, 那么可以通过FileReader对象读取文件, 通过Blob对象创建二进制流, 把数据保存到文件,以及FileWriter也很重要;

    FileReader的API;

    Blob的API;

    FileWriter的API;

  

  通过getFile可以获取到fileEntry对象,fileEntry就是这个文件的对象,我们可以操作文件了哦, 方法如下:createWriter,file:

  通过createWriter这个的回调参数我们可以得到操作这个二进制文件的方法:

  createWriter

Creates a new FileWriter associated with the file that this FileEntry rePResents.

ParameterTypeNullableOptionalDescription
successCallbackFileWriterCallback??A callback that is called with the new FileWriter.
errorCallbackErrorCallback??A callback that is called when errors happen.
Return type: void
  通过file方法,我们就获取到了文件数据, 这个file就是 file类型的input 中获取到的文件对象:

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表