window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
function getFileSystem(type, size) {
return new Promise(function(resolve, reject) {
requestFileSystem(type, size, resolve, reject);
});
}
function getFile(dir, name, params) {
return new Promise(function(resolve, reject) {
dir.getFile(name,params, resolve);
});
}
function writeFile(file, content, type) {
return new Promise(function(resolve, reject) {
file.createWriter(function(writer) {
// writer.addEventListener('error', function(){}, false);
writer.onerror = function(err) {
console.log(err);
};
writer.onwriteend = function(err) {
if(!err) {
console.log('Write end');
}
resolve();
};
writer.onwrite = function() {
console.log('writing...');
};
if(Object.prototype.toString.call(content) !== '[object Array]') {
content = [content.toString()];
}
var blob = new Blob(content, {type: type});
writer.write(blob);
});
});
}
function readFile(fileEntry) {
return new Promise(function(resolve, reject) {
fileEntry.file(function(file){
var reader = new FileReader();
reader.onloadend = function(e) {
resolve(this.result);
}
reader.readAsText(file);
});
});
}
// Writer File
getFileSystem(window.TEMPORARY, 50*1024*1024).then(function(fs) {
return getFile(fs.root,'log.txt',{create:true});
}).then(function(file) {
return writeFile(file, ['This is a good day'], 'text/plain');
}).then(function(content) {
console.log(content);
console.log("filesystem:http://"+window.location.hostname+"/temporary/log.txt");
// var downloadbtn = document.createElement('a');
// document.body.appendChild(downloadbtn);
// downloadbtn.download = "log.txt";
// downloadbtn.href = ("filesystem:http://"+window.location.hostname+"/temporary/answers.html");
//downloadbtn.click();
});
// Reader File
getFileSystem(window.TEMPORARY, 50*1024*1024).then(function(fs) {
return getFile(fs.root,'log.txt',{create:true});
}).then(function(file) {
return readFile(file);
}).then(function(content) {
console.log(content);
});
console