HTML5 本地文件操作之FileSystemAPI实例(一)
文件操作实例整理一
1.请求系统配额类型
console.info(window.TEMPORARY); //0 临时
console.info(window.PERSISTENT); //1 持久
2.持久存储下,创建文件、创建子目录下的文件
//如果使用持久存储,需要使用requestQuota
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
//当前请求存储空间,如果不修改大小的话,只需要请求一次就可以了
console.info(window.webkitStorageInfo);
window.webkitStorageInfo.requestQuota(window.PERSISTENT, 1024 * 1024 * 5, function (gratedBytes) {
console.log('请求成功的空间:' + gratedBytes);
window.requestFileSystem(window.PERSISTENT, gratedBytes, initFs, errorHandler);
}, errorHandler);
function initFs(fs) {
//创建文件,只能创建当前目录下的文件
//如果不指定create=true,文件不存在抛出异常,‘DOMException: A requested file or directory could not be found at the time an operation was processed.’
//如果不指定exclusive,create=true的话,不存在创建,存在重新覆盖
//在文件件目录操作中create=true如果文件目录存在的话不清空
fs.root.getFile('test1.txt', {
create: true, //true:创建新文件,如果文件存在抛出异常执行errorHandle,不影响程序执行
exclusive: true //是否高级文件验证
}, function (fileEntry) {
console.info(fileEntry);
console.log('文件创建成功,' + fileEntry.name);
}, errorHandler); //创建目录下的文件(不能直接指定路径创建文件)
fs.root.getDirectory('MyPictures', { create: true }, function (dirEntry) {
dirEntry.getFile('log3.txt', { create: true }, function (fileEntry) {
console.log('目录下文件创建成功:' + fileEntry.fullPath);
}, errorHandler);
dirEntry.getFile('test1.txt', { create: true }, function (fileEntry) {
console.log('目录下文件创建成功:' + fileEntry.fullPath);
}, errorHandler);
}, errorHandler)
}
function errorHandler(err) {
console.error(err);
//console.info(typeof FileError);//FileError 目前不可用或已经放弃
console.info('创建文件是失败');
}
3.写入文件、读取文件
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(window.TEMPORARY, 5 * 1024, initFs, errorHandler);
function initFs(fs) {
//创建文件,如果不指定exclusive,create=true的话不再创建,存在重新覆盖
fs.root.getFile('test3.txt', { create: true }, function (fileEntity) {
//向文件中写入内容
if (fileEntity.isFile) {
fileEntity.createWriter(function (fileWriter) {
//写入的内容可以可是File 或者Blob
var blob = new Blob(['hello 中国'], {
type: 'text/plain'
});
fileWriter.write(blob);
//显示文件内容
showFile(fileEntity);
}, errorHandler);
}
console.info('当前文件名,' + fileEntity.name);
}, errorHandler);
}
function errorHandler(err) {
console.info('创建文件失败');
console.info(err);
}
//显示指定fileEntity中的内容
function showFile(fileEntity) {
fileEntity.file(function (file) {
var reader = new FileReader();
reader.onloadend = function (e) {
var txt1 = document.getElementById('txt1');
txt1.innerHTML = '写入文件成功:' + reader.result;
}
reader.readAsText(file);
});
}
4.判断文件是否存在
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(window.TEMPORARY, 5 * 1024, initFs, errorHandler);
function initFs(fs) {
//判断文件是否存在
fs.root.getFile('test3.txt', {}, function (fileEntry) {
if (fileEntry.isFile) {
//显示文件内容
showFile(fileEntry);
}
}, errorHandler);
}
function errorHandler(err) {
console.info('创建文件失败');
console.info(err);
}
//显示指定fileEntity中的文件内容、文件信息
function showFile(fileEntry) {
fileEntry.file(function (file) {
console.info(file);
var reader = new FileReader();
reader.onloadend = function (e) {
var txt1 = document.getElementById('txt1');
txt1.innerHTML = '文件名:' + fileEntry.name + '\r\n文件内容:' + reader.result;
//文件大小
txt1.innerHTML += '\r\n字节大小:' + file.size;
}
reader.readAsText(file);
});
}
5.写入文件,并监听事件
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(window.TEMPORARY, 5 * 1024, initFs, errorHandler);
function initFs(fs) {
//特别说明:BlobBuilder 以被抛弃,不建议使用
//写入文件
fs.root.getFile('test4.txt', { create: true }, function (fileEntry) {
if (fileEntry.isFile) {
//使用fileWriter写入内容,并监听事件
fileEntry.createWriter(function (fileWriter) {
console.info(fileWriter);
fileWriter.onwriteend = function (e) {
console.log('读取写入文件结束');
}
fileWriter.onerror = function (e) {
console.log('写入异常:' + e.toString());
}
var blob = new Blob(['<h1>测试内容</h1>'], {
type: 'text/plain'
});
fileWriter.write(blob);
//显示文件
showFile(fileEntry);
}, errorHandler);
}
}, errorHandler);
}
function errorHandler(err) {
console.info('创建文件失败');
console.info(err);
}
//显示指定fileEntity中的文件内容、文件信息
function showFile(fileEntry) {
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function (e) {
var txt1 = document.getElementById('txt1');
txt1.innerHTML = '文件名:' + fileEntry.name + '\r\n文件内容:' + reader.result;
//文件大小
txt1.innerHTML += '\r\n字节大小:' + file.size;
}
reader.readAsText(file);
});
}
更多:
HTML5 本地文件操作之FileSystemAPI整理(二)
HTML5 本地文件操作之FileSystemAPI整理(一)
HTML5 本地文件操作之FileSystemAPI实例(一)的更多相关文章
- HTML5 本地文件操作之FileSystemAPI实例(四)
目录操作Demo二 1.删除目录 window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSyst ...
- HTML5 本地文件操作之FileSystemAPI实例(三)
文件夹操作demo 1.读取根目录文件夹内容 window.requestFileSystem = window.requestFileSystem || window.webkitRequestFi ...
- HTML5 本地文件操作之FileSystemAPI实例(二)
文件操作实例整理二 1.删除文件.复制文件.移动文件 //获取请求权限 window.requestFileSystem = window.requestFileSystem || window.we ...
- HTML5 本地文件操作之FileSystemAPI整理(二)
一.文件目录操作 1.DirectoryEntry对象 属性: 1.isFile: 操作对象的是否为文件,DirectoryEntry对象固定其值为false 2.isDirectory: 操作对象是 ...
- HTML5 本地文件操作之FileSystemAPI整理(一)
一.请求配额 DeprecatedStorageInfo对象 window.webkitStorageInfo:当使用持久存储模式时需要用到该对象的接口 方法: 1.requestQuota(type ...
- HTML5 本地文件操作之FileSystemAPI简介
一.FileSystemAPI简介 HTML5的文件操作Api中 1.FileAPI,用于基础的客户端本地文件读取,目前大多数接口已经被主流浏览器支持,点击查看更多参考 2.FileSystemAPI ...
- H5读取本地文件操作
H5读取本地文件操作 本文转自:转:http://hushicai.com/2014/03/29/html5-du-qu-ben-di-wen-jian.html感谢大神分享. 常见的语言比如php. ...
- 【Egret】实现web页面操作PC端本地文件操作
Egret 实现web页面操作PC端本地文件操作: http://edn.egret.com/cn/book/page/pid/181 //------------------------------ ...
- html5之文件操作
用来把文件读入内存,并且读取文件中的数据.FileReader接口提供了一个异步API,使用该API可以在浏览器主线程中异步访问文件系统,读取文件中的数据.到目前文职,只有FF3.6+和Chrome6 ...
随机推荐
- vmware工具克隆linux系统步骤及配置
我们在学习的时候使用vmware创建自己的虚拟机,但是我们有时学习环境需要多台计算机进行操作演示,如果安装创建虚拟机.再在虚拟机上安装操作系统.这样很花费我们的时间,而且还步能保证服务的一直性,这就用 ...
- Vue.js项目集成ElementUI
Vuejs实例-02Vue.js项目集成ElementUI Vuejs实例-02Vue.js项目集成ElementUI 0:前言 vue.js的UI组件库,在git上有多个项目,我见的使用者比较多 ...
- redis 相关知识点
(1)什么是redis? Redis 是一个基于内存的高性能key-value数据库. (有空再补充,有理解错误或不足欢迎指正) (2)Reids的特点 Redis本质上是一个Key-Value类型的 ...
- div左边固定宽度,右边自适应宽度
样式: <style type="text/css"> html,body { height: 100%; padding: 0; margin: 0; } .oute ...
- 链表用途&&数组效率&&链表效率&&链表优缺点
三大数据结构的实现方式 数据结构 实现方式 栈 数组/单链表 队列 数组/双端链表 优先级队列 数组/堆/有序链表 双端队列 双向链表 数组与链表实现方式的比较 数组与链表都很快 如果能精确预测栈 ...
- C#泛型的抗变与协变
C#泛型的抗变与协变 学习自 C#本质论6.0 https://www.cnblogs.com/pugang/archive/2011/11/09/2242380.html Overview 一直以来 ...
- JS脚本获取开发者后台所有Device
``` var ids = ["Device ID"]; var names = ["Device Name"]; $("td[aria-descri ...
- c#浏览器 遇到的一些问题
禁用弹窗是需要引用一个新的dll,引用方式http://www.itjsxx.com/csharp/IHTMLDocument2_namespace.html, 禁用新的弹窗的方式http://blo ...
- 使用ptrace向已运行进程中注入.so并执行相关函数(转)
1. 简介 使用ptrace向已运行进程中注入.so并执行相关函数,其中的“注入”二字的真正含义为:此.so被link到已运行进程(以下简称为:目标进程)空间中,从而.so中的函数在目标进程空间中有对 ...
- layer.confirm 询问框 的层遮盖
function admin_del(obj) { layer.confirm('确认要重启吗?', { btn : [ '确定', '取消' ]//按钮 }, function(index) { l ...