Skip to content

Instantly share code, notes, and snippets.

@et4891
Created November 26, 2013 04:12
Show Gist options
  • Save et4891/7653352 to your computer and use it in GitHub Desktop.
Save et4891/7653352 to your computer and use it in GitHub Desktop.
File API (Listing Files In Directory) From EionRobb in phonegap IRC
window.requestFileSystem
(
LocalFileSystem.PERSISTENT,
512000,
function(fs)
{
fs.root.createReader().readEntries
(
function(entries)
{
for(var I in entries)console.log(entries[I].name);
}
)
}
);
$(function() {
//writeToFile is a button ID
$("#writeToFile").click(function()
{
//$fileName is an ID of an text input field
var fileName = $("#fileName").val();
var inputText = $("#inputText").val();
writeFile(fileName, inputText);
});
// readFile is a button ID to retrieve texts in the text file
$("#readFile").click(function()
{
var retrieveFileName = $("#retrieveFileName").val();
readFile(retrieveFileName);
});
// readContents is a button ID to retrieve whatever all files/directories inside storage0
$("#readContents").click(function()
{
window.requestFileSystem
(
LocalFileSystem.PERSISTENT,
// in chrome 0 menas temporaray which means data can not be written and 512000 is just size of bytes usable
// but phonegap works a bit differently so even if the number is 0, the API will still work
512000,
function(fs)
{
// to get into a specific directory use the following
// fs.root.getDirectory
// (
// 'directory/name/goes/here', {create:true}, function(dir)
// {
// dir.createReader().readEntries(function...... );
// }
fs.root.createReader().readEntries
(
function(entries)
{
//finds the ul with id named contentList
var ul = document.getElementById('contentList');
// for all the entries inside the directory loop the following
for(var I in entries)
{
// creates li and /li
var li = document.createElement('li');
// creates the name of the contents
li.appendChild(document.createTextNode(entries[I].name));
ul.appendChild(li);
}
// this will only print the newest file created do not use the following if you want to print all the contents in directory
// for(var i in entries)document.getElementById('storageContent').value=entries[i].name;
}
);
}
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment