Skip to content

Instantly share code, notes, and snippets.

@purdrew
Created January 10, 2012 20:15
Show Gist options
  • Save purdrew/1590941 to your computer and use it in GitHub Desktop.
Save purdrew/1590941 to your computer and use it in GitHub Desktop.
Example of using the File API to create/remove a file
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" id="viewport" content="width=device-width,height=device-height,initial-scale=1.0,user-scalable=no">
<script src="json2.js" type="text/javascript"></script>
<script src="phonegap.js" type="text/javascript"></script>
<script type="text/javascript">
// invoked when device is ready
function deviceInfo() {
document.getElementById('window.device.phonegap').innerHTML = 'window.device.phonegap = ' + window.device.phonegap;
}
// register PhoneGap event listeners when DOM content loaded
function init() {
console.log('init()');
document.addEventListener("deviceready", deviceInfo, true);
}
// retrieves root file system entry
var getFileSystemRoot = (function() {
// private
var root;
// one-time retrieval of the root file system entry
var init = function() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function(fileSystem) {
root = fileSystem.root;
},
onFileSystemError);
};
document.addEventListener("deviceready", init, true);
// public function returns private root entry
return function() {
return root;
};
}()); // execute immediately
// file system error handler
function onFileSystemError(error) {
var msg = 'file system error: ' + error.code;
navigator.notification.alert(msg, null, 'File System Error');
}
// logs file events
function onFileEvent(event) {
console.log('file event: ' + event.target.fileName + ' ' + event.type);
}
// called when error reading file
function onFileError(event) {
console.log('file error: ' + event.target.error.code);
}
// called when file is written
function onFileWrite(event) {
onFileEvent(event);
console.log('FileWriter position=' +
event.target.position + ", length=" + event.target.length);
}
// writes a text file to the device
function writeFile() {
// root file system entry
var root = getFileSystemRoot(),
// writes a file
write_file = function(writer) {
var lineCount = 1;
// set the callbacks
writer.onwritestart = onFileEvent;
writer.onprogress = onFileEvent;
writer.onwrite = onFileWrite;
writer.onabort = onFileEvent;
writer.onerror = onFileError;
writer.onwriteend = function(event) {
onFileEvent(event);
lineCount += 1;
if (lineCount <= 3) {
// append a new line
writer.write('Line ' + lineCount + '.\r\n');
}
else {
alert(writer.fileName +
' length=' + writer.length +
', position=' + writer.position);
}
}
// append
writer.seek(writer.length);
// write to file
writer.write('Line ' + lineCount + '.\r\n');
},
// creates a FileWriter object
create_writer = function(fileEntry) {
fileEntry.createWriter(write_file, onFileSystemError);
};
// create a file and write to it
root.getFile('bbgap.txt', {create: true}, create_writer, onFileSystemError);
}
// remove file system entry
function removeFile() {
var root = getFileSystemRoot();
var remove_file = function(entry) {
entry.remove(function() {
navigator.notification.alert(entry.toURI(), null, 'Entry deleted');
}, onFileSystemError);
};
// retrieve a file and truncate it
root.getFile('bbgap.txt', {create: false}, remove_file, onFileSystemError);
}
</script>
<title>File Create/Remove</title>
</head>
<body onload="init()">
<h3>window.device</h3>
<p id="window.device.phonegap">[window.device.phonegap]</p>
<input type="button" value="Write File" onclick="writeFile();return false;" />
<input type="button" value="Remove File" onclick="removeFile();return false;" />
</body>
</html>
@aszel
Copy link

aszel commented Aug 23, 2013

Line #93 is missing a ';' at the end. Sorry for being picky.
thx for the code btw.

@jayeshjain24
Copy link

I have a file which is located at /storage/emulated/0/delete.wav in android. But it always gives error file not found. Any idea?? Do I need to place the file at some specific location so that it can be deleted in future. the problem right now is i vae created my file at this location /storage/emulated/0/myApp/delete.wav

@aya-salama1989
Copy link

@jayeshjain24 Try this

function deleteFile(){

var relativeFilePath = "file:/storage/emulated/0/DCIM/100MEDIA/fileName.ext"; 
alert("remove file");

  function success(entry) {
  alert("Removal succeeded");
}

function fail(error) {
  alert("Error removing file: " + error.code);
}

window.resolveLocalFileSystemURL(relativeFilePath,  function(entry) {
  alert(JSON.stringify(entry));
  entry.remove(success, fail);
});

}

@NesheR16
Copy link

NesheR16 commented Jun 28, 2016

@aya-salama1989 +1

for me 'file://' was missing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment