Search code examples
filecordovadownload

Cordova - Download a file in download folder


I've read a lot of posts but I got no the FINAL answer. Starting from the code at this link, I got my file downloaded into the app. Anyway, I'd like to see it into the "Download" folder. I'm using Android, but clearly I'd like a solution valid for iOS also.


Solution

  • EDIT

    If you already know the path of the file you can just move it:

    var storageLocation = "";
    console.log(device.platform);
    switch (device.platform) {
    
        case "Android":
            storageLocation = 'file:///storage/emulated/0/';
            break;
        case "iOS":
            storageLocation = cordova.file.documentsDirectory;
            break;
    
    }
    
    
    var fileUri = "file:///data/user/0/com.arsdigitalia.myapp/files/files/MyApp‌​/test.pdf"
    
    function moveFile(fileUri) {
        window.resolveLocalFileSystemURL(
              fileUri,
              function(fileEntry){
    
                    var parentEntry = storageLocation + "Download";
                   
                    // move the file to a new directory and rename it
                   fileEntry.moveTo(parentEntry, "newFile.pdf", success, fail);
                           
              },
              errorCallback);
    }

    Original

    Here is a sample piece of code I use to accomplish this. It works best on Android, iOS is a bit different due to the app sandboxing so you need to handle retrieving files yourself. I also use the Cordova device plugin to determine what device the app is running on, I can then change storage paths to suit:

    var storageLocation = "";
    console.log(device.platform);
    switch (device.platform) {
    
        case "Android":
            storageLocation = 'file:///storage/emulated/0/';
            break;
        case "iOS":
            storageLocation = cordova.file.documentsDirectory;
            break;
    
    }
    
    window.resolveLocalFileSystemURL(storageLocation,
        function (fileSystem) {
    
            fileSystem.getDirectory('Download', {
                    create: true,
                    exclusive: false
                },
                function (directory) {
    
                    //You need to put the name you would like to use for the file here.
                    directory.getFile("YOUR_FILE_NAME", {
                            create: true,
                            exclusive: false
                        },
                        function (fileEntry) {
    
    
                            fileEntry.createWriter(function (writer) {
                                writer.onwriteend = function () {
                                    console.log("File written to downloads")
                                };
    
                                writer.seek(0);
                                writer.write(YOUR_FILE_HERE); //You need to put the file, blob or base64 representation here.
    
                            }, errorCallback);
                        }, errorCallback);
                }, errorCallback);
        }, errorCallback);
    
    var errorCallback = function(e) {
        
        console.log("Error: " + e)
        
    }

    Then to retrieve the file list from the directory you can use:

    window.resolveLocalFileSystemURL(storageLocation,
        function (fileSystem) {
        
            fileSystem.getDirectory('Download', {
                    create: true,
                    exclusive: false
                },
                function (directory) {
    
                    var reader = directory.createReader();
                    reader.readEntries(function (files) {
    
                        if (files.length == 0) {
    
                            console.log("No files found in downloads folder.")
    
                        } else {
    
                            $.each(files, function (i, v) {
    
                                console.log("File Name: " + files[i].name;)
    
                            });
    
                        }
    
                    }, getFilesFail);
                }, getFilesFail);
        }, getFilesFail);
    
    var getFilesFail = function(e) {
        
        console.log("Error: " + e);
        
    }

    To install the device plugin use this command:

    cordova plugin add cordova-plugin-device
    

    Documentation here:

    https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-device/