I\'m writing an Android application with Phonegap 1.4.1 and Sencha that downloads and reads pdf files. How can I check whether the file exists in the phone directory using e
note:
when I get the filesystem I save it in a var under a macro pg object:
pg = {fs:{}} // I have a "GOTFS" function... a "fail" function
pg.fs.filesystem = window.requestFileSystem(window.PERSISTENT, 0, pg.fs.GOTFS, pg.fs.fail);
so my code is pretty simple...
var fileExists = function(path, existsCallback, dontExistsCallback){
pg.fs.fileSystem.root.getFile(path, { create: false }, existsCallback, dontExistsCallback);
// "existsCallback" will get fileEntry as first param
}
The trouble with all of the current answers is that they depend on an asynchronous callback which updates a global variable. If you are checking multiple files, there is a risk that the variable will have been set by a different callback.
A basic Javascript XMLHttpRequest check is perfect for synchronously checking the file is accessible to via Javascript.
function checkFileExists(fileName){
var http = new XMLHttpRequest();
http.open('HEAD', fileName, false);
http.send(null);
return (http.status != 404);
}
Just pass in the full path to the file and then you can reliably use:
if (checkFileExists(fullPathToFile)) {
// File Exists
} else {
// File Does Not Exist
}
To store the root path in a variable you can use:
var fileSystemRoot; // Global variable to hold filesystem root
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fsSuccess, fsError);
function fsError() {
console.log("Failed to get Filesystem");
}
function fsSuccess(fileSystem) {
console.log("Got Filesystem: Root path " + fileSystem.root);
// save the file to global variable for later access
window.fileSystemRoot = fileSystem.root;
}
I had the same problem. I could not manage to get Darkaico's answer to work, but with the answer of Kurt I could make it work.
Here's my code:
function checkIfFileExists(path){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
fileSystem.root.getFile(path, { create: false }, fileExists, fileDoesNotExist);
}, getFSFail); //of requestFileSystem
}
function fileExists(fileEntry){
alert("File " + fileEntry.fullPath + " exists!");
}
function fileDoesNotExist(){
alert("file does not exist");
}
function getFSFail(evt) {
console.log(evt.target.error.code);
}
Then you only need to execute like this:
checkIfFileExists("path/to/my/file.txt");
Kurt and Thomas gave better answers, because Darkaico's function not only checks if the file exists but also opens the file and reads it until the end.
This isn't a problem with small files, but if you check a large file then the app may crash.
Anyway, please use .getFile method - it's the best option.
var fileexist;
function checkIfFileExists(path){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
fileSystem.root.getFile(path, { create: false }, fileExists, fileDoesNotExist);
}, getFSFail); //of requestFileSystem
}
function fileExists(fileEntry){
alert("File " + fileEntry.fullPath + " exists!");
fileexist = true;
}
function fileDoesNotExist(){
alert("file does not exist");
fileexist = false;
}
function getFSFail(evt) {
console.log(evt.target.error.code);
fileexist=false;
}
now you can use conditions
if(fileexist=="true"){
//do something;
}
else if(fileexist=="false"){
//do something else
}
This code can be used for custom cases, full documentation here: download if not exist
document.addEventListener("deviceready", init, false);
//The directory to store data
var store;
//Used for status updates
var $status;
//URL of our asset
var assetURL = "https://raw.githubusercontent.com/cfjedimaster/Cordova-Examples/master/readme.md";
//File name of our important data file we didn't ship with the app
var fileName = "mydatafile.txt";
function init() {
$status = document.querySelector("#status");
$status.innerHTML = "Checking for data file.";
store = cordova.file.dataDirectory;
//Check for the file.
window.resolveLocalFileSystemURL(store + fileName, appStart, downloadAsset);
}
function downloadAsset() {
var fileTransfer = new FileTransfer();
console.log("About to start transfer");
fileTransfer.download(assetURL, store + fileName,
function(entry) {
console.log("Success!");
appStart();
},
function(err) {
console.log("Error");
console.dir(err);
});
}
//I'm only called when the file exists or has been downloaded.
function appStart() {
$status.innerHTML = "App ready!";
}