Skip to content

Instantly share code, notes, and snippets.

@secunit64
Created March 19, 2012 10:36
Show Gist options
  • Select an option

  • Save secunit64/2107014 to your computer and use it in GitHub Desktop.

Select an option

Save secunit64/2107014 to your computer and use it in GitHub Desktop.
HTML5 Filesystem API usage- uses filerjs
///HTML5 Filesystem API
input(type="file", id="myfile", multiple)
script(id='myscript', type='text/javascript')
document.querySelector('#myfile').onchange = function(e){
//Open the FS, otherwise copy the files into the FS
if(filer && !filer.isOpen){
openFS(this.files);
} else {
importToFS(this.files);
}
}
//global error handler
function onError(e){
console.log('Encountered error ' + e.name);
}
//on init handler for filesystems
function onInit(fs){
//"this" object refers to "files"
console.log('Filesystem initialized');
importToFS(this);
}
//opening an html5 filesystem (100MB)
//filesystem handle is the global var "filer"
function openFS(files){
filer.init({
persistent: false,
size: 100*(1024*1024)
},
onInit.bind(files),
onError
);
}
//import function to copy selected files into the app filesystem
//NOTE: There is no quota check, so the call is bound to fail after
//a point.
function importToFS(files){
Util.toArray(files).forEach(function(file, i){
console.log('Importing: '+file.name);
filer.write(file.name, {data: file, type: file.type}, function(fileEntry, fileWriter){
//open the crunch file
filer.open(fileEntry, function(file){
crunchReader.readAsText(file);
}, onError);
}, onError);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment