Mike
Mike

Reputation: 1261

Phonegap Cordova file move issues

I have look threw most of the post and found the below code which I have modified to fit my needs. But I can't seem to get it to return the full new path of the file. and the file is not moved?

function successCallback(entry) {



    alert("Success. New Path: " + entry.fullPath);
}

function errorCallback(error) {
    console.log("Error:" + error.code)
    alert(error.code);
}


function moveFile(fileUri) {

    window.resolveLocalFileSystemURL(
          fileUri,
          function(fileEntry){
                newFileUri  = cordova.file.dataDirectory + "images/";
                oldFileUri  = fileUri;
                fileExt     = "." + oldFileUri.split('.').pop();
                newFileName = uniqueId() + fileExt;

                window.resolveLocalFileSystemURL(newFileUri ,
                        function(dirEntry) {
                            // move the file to a new directory and rename it
                    alert(dirEntry.fullPath)
                            fileEntry.moveTo(dirEntry, newFileName, successCallback, errorCallback);
                        },
                        errorCallback);
          },
          errorCallback);
}

now the successCallback gets called but the new file path is wrong, and the file is not moved.

Upvotes: 1

Views: 183

Answers (1)

Mike
Mike

Reputation: 1261

This is how I did it

function moveFile(fileUri) {
    window.resolveLocalFileSystemURI(fileUri, successCallback, errorCallback);
}

function successCallback(entry) {

    var newFileName = uniqueId() + '.jpg';

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
        function (fileSys) {
            fileSys.root.getDirectory("reunion", {
                    create: true,
                    exclusive: false
                },
                function (directory) {
                    window.rootFS = fileSys.root;
                    entry.moveTo(directory, newFileName, FileMoved, errorCallback);
                }, errorCallback);
        }, errorCallback);
}


function FileMoved(entry) {

     var image = window.rootFS.toURL() + entry.fullPath;
     var d = new Date();

    var html = '<img class="imgPost" data-count="' + window.ImageCount + '" id="img' + window.ImageCount + '" width="100%" src="' + image + '?' + d.getTime() + '" />';
    html += '<a id="deleteImgBnt' + window.ImageCount + '" href="#" data-count="' + window.ImageCount + '" class="deletephoto ui-btn ui-corner-all ui-shadow ui-btn-inline ui-btn-d" data-role="button" data-theme="a" data-icon="delete" data-iconpos="right"> Delete Image </a> ';

    $('#photo').prepend(html);
}

function errorCallback(error) {
    console.log("Error:" + error.code)
    alert(error.code);
}

function uniqueId() {
    // desired length of Id
    var idStrLen = 32;
    // always start with a letter -- base 36 makes for a nice shortcut
    var idStr = (Math.floor((Math.random() * 25)) + 10).toString(36) + "_";
    // add a timestamp in milliseconds (base 36 again) as the base
    idStr += (new Date()).getTime().toString(36) + "_";
    // similar to above, complete the Id using random, alphanumeric characters
    do {
        idStr += (Math.floor((Math.random() * 35))).toString(36);
    } while (idStr.length < idStrLen);

    return (idStr);
}
function moveFile(fileUri) {
    window.resolveLocalFileSystemURI(fileUri, successCallback, errorCallback);
}

function successCallback(entry) {

    var newFileName = uniqueId() + '.jpg';

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
        function (fileSys) {
            fileSys.root.getDirectory("reunion", {
                    create: true,
                    exclusive: false
                },
                function (directory) {
                    window.rootFS = fileSys.root;
                    entry.moveTo(directory, newFileName, FileMoved, errorCallback);
                }, errorCallback);
        }, errorCallback);
}


function FileMoved(entry) {

     var image = window.rootFS.toURL() + entry.fullPath;
     var d = new Date();

    var html = '<img class="imgPost" data-count="' + window.ImageCount + '" id="img' + window.ImageCount + '" width="100%" src="' + image + '?' + d.getTime() + '" />';
    html += '<a id="deleteImgBnt' + window.ImageCount + '" href="#" data-count="' + window.ImageCount + '" class="deletephoto ui-btn ui-corner-all ui-shadow ui-btn-inline ui-btn-d" data-role="button" data-theme="a" data-icon="delete" data-iconpos="right"> Delete Image </a> ';

    $('#photo').prepend(html);
}

function errorCallback(error) {
    console.log("Error:" + error.code)
    alert(error.code);
}

function uniqueId() {
    // desired length of Id
    var idStrLen = 32;
    // always start with a letter -- base 36 makes for a nice shortcut
    var idStr = (Math.floor((Math.random() * 25)) + 10).toString(36) + "_";
    // add a timestamp in milliseconds (base 36 again) as the base
    idStr += (new Date()).getTime().toString(36) + "_";
    // similar to above, complete the Id using random, alphanumeric characters
    do {
        idStr += (Math.floor((Math.random() * 35))).toString(36);
    } while (idStr.length < idStrLen);

    return (idStr);
}

Upvotes: 1

Related Questions