来着火狐开发网络的官方文档:点我打开;
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是在对象上面的方法, 这个主要是处理目录,比如打开目录, 或者新建目录:
Parameter | Type | Nullable | Optional | Description |
---|---|---|---|---|
path | DOMString | ? | ? | 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. |
options |
| ? | ? |
|
successCallback |
| ? | ? | A callback that is called to return the DirectoryEntry selected or created. |
errorCallback |
| ? | ? | A callback that is called when errors happen. |
getFile这个是获取文件, 作用是打开文件或者新建文件;
Parameter | Type | Nullable | Optional | Description |
---|---|---|---|---|
path | DOMString | ? | ? | 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. |
options |
| ? | ? |
|
successCallback |
| ? | ? | A callback that is called to return the File selected or created. |
errorCallback |
| ? | ? | 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.
Parameter | Type | Nullable | Optional | Description |
---|---|---|---|---|
successCallback |
| ? | ? | A callback that is called with the new FileWriter. |
errorCallback |
| ? | ? | A callback that is called when errors happen. |
void
新闻热点
疑难解答