Skip to content

Instantly share code, notes, and snippets.

@dhavaln
Created June 8, 2012 08:51
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save dhavaln/2894564 to your computer and use it in GitHub Desktop.
Save dhavaln/2894564 to your computer and use it in GitHub Desktop.
PhoneGap Filesystem Example
/**
* Prepare the App Folder
*/
(function(){
window.appRootDirName = ".myapp";
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
console.log("device is ready");
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function fail() {
console.log("failed to get filesystem");
}
function gotFS(fileSystem) {
console.log("filesystem got");
fileSystem.root.getDirectory(window.appRootDirName, {
create : true,
exclusive : false
}, dirReady, fail);
}
function dirReady(entry) {
window.appRootDir = entry;
console.log(JSON.stringify(window.appRootDir));
}
})();
function downloadImage(url, success, err){
console.log('download image ' + url);
var fileName = new Date().getTime() + ".png";
ft = new FileTransfer();
ft.download(
url,
window.appRootDir.fullPath + "/" + fileName,
function(entry) {
console.log("download complete: " + entry.fullPath);
success(entry.fullPath);
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
err(error);
}
);
}
@tekmonk
Copy link

tekmonk commented Aug 10, 2012

very nice! Didnt see root.getDirectory anywhere in the docs so was having some trouble getting the sd card location. Works in Cordova 2.0!

@sampathpremarathna
Copy link

Excellent! .Thank you very much for shearing this valuable code.

@amitaibu
Copy link

I believe that in Phonegap 3, the appRootDirName is no longer needed.

@senthilmurugan-tag
Copy link

i am using cordova 3.3.0. I am able to play a local file (stored in app /www folder) in android. But for ios it will return the error "cannot play this file ....".

Reply me how to mention the local file (audio) path in ios. ?

@praveen4565
Copy link

We have implemented filesystem.js in cordova application which is getting into fail callback function for Android Version Marshmallow 6.0.1.
Could you please help us to know is there any option to resolve this issue??

@charyorde
Copy link

exclusive : false means what?

@amrayoub
Copy link

@volkergraf
Copy link

Hell .. Tried the examples but I'm only getting "/" as RootPath on IOS 10.x

@taufiksu
Copy link

taufiksu commented Jul 20, 2017

Thanks, this code help me to create cordova function downloader. https://gist.github.com/taufiksu/e286a438dc0725ad60a0f1d49cedd1f6

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