Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download images and save locally on iPhone Phonegap app

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.

like image 628
StJimmy Avatar asked Sep 02 '10 09:09

StJimmy


1 Answers

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=="
            }
        }
    );
like image 120
Mister Smith Avatar answered Nov 05 '22 02:11

Mister Smith