Reputation: 2724
In cordova 3.5 I have a basePath for my app defined as
basePath = 'cdvfile://localhost/persistent/myapp/';
I am able to use the fileTransfer plugin to store some downloaded images. In android the end up being in the sdcard/myapp/ which is good. [I have both required file and filetransfer plugins]. In iPhone I don't know the location but somehow they are stored correctly.
I checked the cordova / phonegap documentation and also other stackoverflow questions but cannot find anything useful.
However I am unable trying to list the same directory. I tried:
function success (entries) {
var i;
for (i = 0; i < entries.length; i++) {
console.log(entries[i].name);
}
}
function fail (error) {
console.log("Failed to list directory contents: " + JSON.stringify(error));
}
var parent = 'cdvfile://localhost/persistent/';
var parentName = "pogedapp";
var directoryEntry = new DirectoryEntry(parentName, parent);
var directoryReader = directoryEntry.createReader();
directoryReader.readEntries(success, fail);
The error return is code 5 which according to this page [ https://developer.mozilla.org/en-US/docs/Web/API/FileError ] should be 'The URL is malformed. Make sure that the URL is complete and valid.'
Is there a way to make my code working ? I should be able for example to download images locally and then delete them at a later stage. So both the directory listing and the file transfer should use the same folder. I managed to get a listing of the Root folder but I assume that I cannot use that one as the final directory is different from Android / iOS.
Upvotes: 0
Views: 1907
Reputation: 78
We had the same issue and we got it solved by ignoring the localhost path. you should be calling it this way.. remember the images are read as Base65 content. so, you have to read them as data URLs. you should get it working. following is my code block which worked perfectly.
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
function gotFS(fileSystem) {
fileSystem.root.getFile("yourdirectory/"+numImg[0], null, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.file(gotFile, fail);
}
function gotFile(file){
readDataUrl(file);
}
function readDataUrl(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
imgArray.push(evt.target.result);
};
reader.readAsDataURL(file);
}
Upvotes: 1