文件操作实例整理二

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实例(二)的更多相关文章

  1. HTML5 本地文件操作之FileSystemAPI实例(四)

    目录操作Demo二 1.删除目录 window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSyst ...

  2. HTML5 本地文件操作之FileSystemAPI实例(三)

    文件夹操作demo 1.读取根目录文件夹内容 window.requestFileSystem = window.requestFileSystem || window.webkitRequestFi ...

  3. HTML5 本地文件操作之FileSystemAPI实例(一)

    文件操作实例整理一 1.请求系统配额类型 console.info(window.TEMPORARY); //0 临时 console.info(window.PERSISTENT); //1 持久 ...

  4. HTML5 本地文件操作之FileSystemAPI整理(二)

    一.文件目录操作 1.DirectoryEntry对象 属性: 1.isFile: 操作对象的是否为文件,DirectoryEntry对象固定其值为false 2.isDirectory: 操作对象是 ...

  5. HTML5 本地文件操作之FileSystemAPI整理(一)

    一.请求配额 DeprecatedStorageInfo对象 window.webkitStorageInfo:当使用持久存储模式时需要用到该对象的接口 方法: 1.requestQuota(type ...

  6. HTML5 本地文件操作之FileSystemAPI简介

    一.FileSystemAPI简介 HTML5的文件操作Api中 1.FileAPI,用于基础的客户端本地文件读取,目前大多数接口已经被主流浏览器支持,点击查看更多参考 2.FileSystemAPI ...

  7. H5读取本地文件操作

    H5读取本地文件操作 本文转自:转:http://hushicai.com/2014/03/29/html5-du-qu-ben-di-wen-jian.html感谢大神分享. 常见的语言比如php. ...

  8. 【Egret】实现web页面操作PC端本地文件操作

    Egret 实现web页面操作PC端本地文件操作: http://edn.egret.com/cn/book/page/pid/181 //------------------------------ ...

  9. html5之文件操作

    用来把文件读入内存,并且读取文件中的数据.FileReader接口提供了一个异步API,使用该API可以在浏览器主线程中异步访问文件系统,读取文件中的数据.到目前文职,只有FF3.6+和Chrome6 ...

随机推荐

  1. 跟厂长学PHP7内核(四):生命周期之开始前的躁动

    上一章我们对PHP的源码目录结构有了初步了解,本章我们继续从生命周期的维度对PHP进行剖析. 一.概览 生命周期是什么呢?你可以把它看作执行过程,PHP的生命周期也就是它从开始执行到结束执行的过程. ...

  2. canvas版《俄罗斯方块》

    试玩(没有考虑兼容低版本浏览器): See the Pen Canvas俄罗斯方块 by 王美建 (@wangmeijian) on CodePen. ************************ ...

  3. 通过css属性hack完成select样式美化,并兼容IE

    最近在重构时遇到了select样式问题,并且需要在不影响语义化的情况下,兼容IE8. 经过一番的百度后始终没有找到合适的纯CSS解决方案,最后换了一下思路,大胆使用了属性hack: 在chrome和F ...

  4. 【HDU 3590】 PP and QQ (博弈-Anti-SG游戏,SJ定理,树上删边游戏)

    PP and QQ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. Codeforces.314E.Sereja and Squares(DP)

    题目链接 http://www.cnblogs.com/TheRoadToTheGold/p/8443668.html \(Description\) 给你一个擦去了部分左括号和全部右括号的括号序列, ...

  6. hdu 5792 World is Exploding 树状数组

    World is Exploding 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...

  7. Mysql_解决The total number of locks exceeds the lock table size错误

    在操作mysql数据库表时出现以下错误. 网上google搜索相关问题,发现一位外国牛人这么解释: If you're running an operation on a large number o ...

  8. sublime text2 用ctags插件实现方法定位(转)

    我们用sublime几乎都会首先安装这个插件,这个插件是管理插件的功能,先安装它,再安装其他插件就方便了. 点击sublime的菜单栏 view->show console :现在打开了控制台, ...

  9. MSDN WinUSB Example

    The WinUSB user-mode library uses device interface classes to communicate with the kernel-mode USB s ...

  10. bitnami下webmin安装

    下载 我在官方网站下载最新的安装包(webmin_1.670_all.deb):http://sourceforge.net/projects/webadmin/files/webmin  安装 单独 ...