HTML5 本地文件操作之FileSystemAPI实例(二)
文件操作实例整理二
1.删除文件、复制文件、移动文件
//获取请求权限
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(window.TEMPORARY, 5 * 1024, initFs, errorHandler);
function initFs(fs) {
//删除文件 fileEntry.remove()
fs.root.getFile('test2.txt', { create: false }, function (fileEntry) {
fileEntry.remove(function () {
console.log('删除文件成功');
}, errorHandler);
}, errorHandler);
//复制文件,如果文件存在则覆盖
fs.root.getFile('test4.txt', { create: false }, function (fileEntry) {
fileEntry.copyTo(fs.root, 'text4_copy.txt', function (fileEntry) {
console.info(fileEntry);
console.log('复制文件成功:' + fileEntry.fullPath);
}, errorHandler);
}, errorHandler)
//移动文件,如果文件不存在移动失败
fs.root.getFile('test3.txt', { create: false }, function (fileEntry) {
//获取移动目录
fs.root.getDirectory('mymove', { create: true }, function (dirEntry) {
//移动文件如果
fileEntry.moveTo(dirEntry, 'test4_move.txt', function (fileEntry) {
console.log('移动文件成功:' + fileEntry.fullPath);
}, errorHandler);
}, errorHandler);
}, errorHandler);
}
function errorHandler(err) {
console.info('创建文件失败');
console.info(err);
}
2.写入追加文件
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(window.TEMPORARY, 5 * 1024, initFs, errorHandler);
function initFs(fs) {
//指定位置,追加内容
fs.root.getFile('test3.txt', { create: true }, function (fileEntity) {
//向文件中写入内容
if (fileEntity.isFile) {
fileEntity.createWriter(function (fileWriter) {
//将写入指针移动到文件结尾
fileWriter.seek(fileWriter.length); var blob = new Blob(['hello 中国 \r\n'], {
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);
});
}
3.截断文件可以使用fileWriter.trancute()
4.读取根目录中的图片,显示到网页
//获取请求权限
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(window.TEMPORARY, 5 * 1024, initFs, errorHandler);
function initFs(fs) {
//读取根目录的图片,并显示
var dirReader = fs.root.createReader();
var entries = [];
var readEntries = function () {
dirReader.readEntries(function (results) {
if (results.length == 0) {
showEntries(entries.sort());
} else {
entries = entries.concat(toArray(results));
readEntries();
}
}, errorHandler);
}
readEntries();
}
function errorHandler(err) {
console.info('创建文件失败');
console.info(err);
}
function toArray(list) {
return Array.prototype.slice.call(list || [], 0);
}
//显示图片
function showEntries(entries) {
var fragment = document.createDocumentFragment();
entries.forEach(function (entry, i) {
if (entry.isFile && entry.name.indexOf('.png') != -1) {
//将entry.toURL()结果:filesystem:http://localhost:57128/temporary/2017-02-23_182417.png
console.info(entry.toURL());
var li = document.createElement('li');
li.innerHTML = ['是否是目录:', entry.isDirectory, '----文件名:', entry.name, '<img src="' + entry.toURL() + '" width="100" border=1 />'].join('');
fragment.appendChild(li);
}
});
document.querySelector('#img').appendChild(fragment);
}

5.选择多个文件,并复制到沙盒文件系统中
document.querySelector('#myFile').onchange = function (e) {
var files = this.files;
//获取请求权限
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(window.TEMPORARY, 5 * 1024, initFs, errorHandler);
function initFs(fs) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
(function (f) {
//复制文件,如果存在抛出异常
fs.root.getFile(file.name, { create: true, exclusive: true }, function (fileEntry) {
fileEntry.createWriter(function (fileWriter) {
fileWriter.write(f); //write() 参数可以使File或Blob对象
});
//显示文件
showFile(fileEntry);
}, errorHandler)
})(file);
}
}
};
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 += '\r\n文件名:' + fileEntry.name + '\r\n文件内容:' + reader.result;
//文件大小
txt1.innerHTML += '\r\n字节大小:' + file.size;
}
reader.readAsText(file);
});
}
更多:
HTML5 本地文件操作之FileSystemAPI实例(一)
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.请求系统配额类型 console.info(window.TEMPORARY); //0 临时 console.info(window.PERSISTENT); //1 持久 ...
- 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 ...
随机推荐
- max 基础知识
MDX基本语法 MD询语句的结构及语法 MDX查询示例 基本的MDX SELECT语句包含一SELELCT字句和一个FROM字句,以及一个可选的WHERE子句.如下 SELECT {[Measures ...
- R语言编程艺术(4)R对数据、文件、字符串以及图形的处理
本文对应<R语言编程艺术> 第8章:数学运算与模拟: 第10章:输入与输出: 第11章:字符串操作: 第12章:绘图 =================================== ...
- Spring Cache缓存技术的介绍
缓存用于提升系统的性能,特别适用于一些对资源需求比较高的操作.本文介绍如何基于spring boot cache技术,使用caffeine作为具体的缓存实现,对操作的结果进行缓存. demo场景 本d ...
- 跟厂长学PHP7内核(八):深入理解字符串的实现
在前面大致预览了常用变量的结构之后,我们今天来仔细的剖析一下字符串的具体实现. 一.字符串的结构 struct _zend_string { zend_refcounted_h gc; /* 字符串类 ...
- bzoj4001: [TJOI2015]概率论
题目链接 bzoj4001: [TJOI2015]概率论 题解 生成函数+求导 设\(g(n)\)表示有\(n\)个节点的二叉树的个数,\(g(0) = 1\) 设\(f(x)\)表示\(n\)个节点 ...
- bzoj 4000 矩阵快速幂优化DP
建立矩阵,跑快速幂 /************************************************************** Problem: 4000 User: idy002 ...
- [Java]Spring框架
在这里学习Spring框架: >>spring&struts框架学习 >>spring >>Java回顾之Spring基础 >>IBM Java ...
- (67)Wangdao.com第十一天_JavaScript 数组的遍历
for 普通方式遍历 var arr = [0,1,2,3,4,5,6]; for(i=0; i<arr.length; i++){ document.write("["+i ...
- 使用 IntraWeb (22) - 基本控件之 TIWCalendar
TIWCalendar: 日历控件, 继承于 TIWCustomGrid, 所以它和 TIWGrid 共同属性特多. 它的 Cell 是 TIWCalendarCell 对象, 直接从 TIWGrid ...
- 关于 C 语言,我喜欢和讨厌的十件事
前言:最近有个家伙抱怨道“为什么我还要再用C?”-虽然我不同意他的说法,但至少他随口提到如果你“在一台拇指大小的电脑”上编程,或者为一门语言写引导程序,那么可以用C语言.要我说,写设备驱动,或者特定平 ...