Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileSystem on Cordova 3.4.0 fails "Could not create target file"

I recently upgraded my iOS Cordova project from 2.7.0 to 3.4.0.

After upgrading filesystem access is broken. (seems to work in the simulator though?)

I get an error message stating "Could not create target file", I googled around and thought to change my "fullpath" to "toURL()" but to no avail. I really don't know what to try next?

here's my download code

window.requestFileSystem(
LocalFileSystem.PERSISTENT, 0,

function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile(
    "dummy.html", {
    create: true,
    exclusive: false
},

function gotFileEntry(fileEntry) {
    var sPath = fileEntry.toURL().replace("dummy.html", "");
    var fileTransfer = new FileTransfer();
    fileEntry.remove();

    fileTransfer.download(
        "https://dl.dropbox.com/u/13253550/db02.xml",
    sPath + "database.xml",

    function (theFile) {
        console.log("download complete: " + theFile.toURI());
        showLink(theFile.toURI());
        setTimeout(function () {
            checkConnection();
        }, 50);
    },

    function (error) {
        console.log("download error source " + error.source);
        console.log("download error target " + error.target);
        console.log("upload error code: " + error.code);
    });
},
fail);
},
fail);
like image 974
Hessius Avatar asked Mar 06 '14 01:03

Hessius


1 Answers

I found the documentation for both the file plugin ( link) and the fileTransfer plugin ( link)

After making the change noted in the original question, I wondered if the file plugin part was OK and started looking for discrepancies between my fileTransfer code and the examples provided.

Turns out I wasn't doing encodeURI() on my download source url (doh)

so the complete, working code:

window.requestFileSystem(
LocalFileSystem.PERSISTENT, 0,

function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile(
"dummy.html", {
create: true,
exclusive: false
},

function gotFileEntry(fileEntry) {
var sPath = fileEntry.toURL().replace("dummy.html", "");
var fileTransfer = new FileTransfer();
fileEntry.remove();
var DBuri = encodeURI("https://dl.dropbox.com/u/13253550/db02.xml");
fileTransfer.download(
    DBuri,
sPath + "database.xml",

function (theFile) {
    console.log("download complete: " + theFile.toURI());
    showLink(theFile.toURI());
    setTimeout(function () {
        checkConnection();
    }, 50);
},

function (error) {
    console.log("download error source " + error.source);
    console.log("download error target " + error.target);
    console.log("upload error code: " + error.code);
});
},
fail);
},
fail);
like image 153
Hessius Avatar answered Jan 01 '23 13:01

Hessius