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 ...
随机推荐
- 交叉验证(Cross Validation)简介
参考 交叉验证 交叉验证 (Cross Validation)刘建平 一.训练集 vs. 测试集 在模式识别(pattern recognition)与机器学习(machine lea ...
- Codeforces 348B - Apple Tree
348B - Apple Tree 我们设最后答案为 x , 我们我们就能用x表示出所有节点下面的苹果个数, 然后用叶子节点求lcm, 取最大的可行解. #include<bits/stdc++ ...
- SpringBoot的Controller使用
一: 1.注解 2.control注解 3.效果 4.RespomseBody package com.caojun.springboot; import org.springframework.be ...
- [代码审计]SRCMS的两点小越权
0x00 简介 SRCMS是一个开源的企业安全应急响应中心,基于ThinkPHP 3.2框架开发.该系统在2017-09-09已经停止更新了,主要是在翻看p神博客文章时看到这个,随想自己再审一次. p ...
- 用mac的chrome浏览器调试Android手机的网页
一.参考链接 read chrome remote debugging documentation 调出开发者选项 二.设置android 在安卓4.2及更新的版本中,默认情况下,[开发者选项]是隐藏 ...
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem H. Password Service dp
Problem H. Password Service 题目连接: http://www.codeforces.com/gym/100253 Description Startups are here ...
- SQL Server Management Studio 教程二: 创建新登录名
1.先用windows身份登录SQL server2008 2.打开[安全性],右击[登录名],选择[新建登录名] 3.[常规]选项页面中,修改如下位置设置,默认数据库可以是其他数据库,不一定是mas ...
- LPC-LINK 2 Board IO TABLE
- rcp(插件开发)插件B需要引用插件A中的jar包-如何处理依赖关系
如果插件B需要引用插件A中的jar 通常需要以下几步: 1.插件B要依赖插件A 2.在插件B的build path中添加插件A的jar包 3.插件A的runtime导出插件B中使用jar的packag ...
- UVA12304-2D Geometry 110 in 1!
就是给了六个关于圆的算法.实现它们. 注意的是,不仅输出格式那个符号什么的要一样.坐标的顺序也要从小到大-- 基本上没考虑什么精度的问题,然后就过了. 大白鼠又骗人.也许我的方法比較好? 我的做法就是 ...