krmax44 krmax44 - 4 months ago 18
HTML Question

Cordova - Download image and move it into another folder

I'm currently trying to download an image and then moving it to a directory on the sdcard.

I have this code to download the image:

utils.download = function (url, success, fail) {
var fileTransfer = new FileTransfer();
var uri = encodeURI(url);
var store = cordova.file.dataDirectory + "_temp/";
fileTransfer.download(uri,
store + "wallpaper.jpg",
function(res){success(res);},
function(res){fail(res);});
};


This works perfectly fine.

Now I need to move the file (to
file://mnt/sdcard/FolderName/wallpaper.png
). But there's not a single word about moving files in the Cordova file documentation, so I'm very confused on how to do this... Anybody can help me there?

Thank you so much!

Answer

Prototype of function : Line 107 @ cordova-file-plugin

Untested example. but will most likely work!

function moveFile(fileUri, destDir) {
    window.resolveLocalFileSystemURL(
        fileUri,
        function(fileEntry) {
            newFileName = "myfile.data";
            window.resolveLocalFileSystemURL(destDir,
                function(dirEntry) {
                    fileEntry.moveTo(dirEntry, newFileName, successCallback, errorCallback);
                },
                errorCallback);
        },
        errorCallback);
}
Comments