Download images and save locally on iPhone Phonegap app

StJimmy picture StJimmy · Sep 2, 2010 · Viewed 23.4k times · Source

I've already managed to save a web page (x/html) successfully, but I'd also like to save the images and mp4 videos that are contained in it, for further visualization in offline mode.

I've got access to the iOS filesystem, so I save the html by obtaining the code through an AJAX request, and later saving it to a file.

I don't really know how to do the same with video and images. I have a server to which I can send queries from my app, so it shows exclusively the content I need to download, with the optimal headers in case its necessary. I just don't know how to "download" it from the client side (Javascript).

Thanks in advance for any help.

Answer

Mister Smith picture Mister Smith · May 29, 2013

You can use a FileTransfer object to download a remote image to a local file.

This is the latest official sample snippet:

    // !! Assumes filePath is a valid path on the device

    var fileTransfer = new FileTransfer();
    var uri = encodeURI("http://some.server.com/download.php");

    fileTransfer.download(
        uri,
        filePath,
        function(entry) {
            console.log("download complete: " + entry.fullPath);
        },
        function(error) {
            console.log("download error source " + error.source);
            console.log("download error target " + error.target);
            console.log("upload error code" + error.code);
        },
        false,
        {
            headers: {
                "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
            }
        }
    );