Search code examples
ioscordovapdfdownload

Cordova iOS direct download pdf


I'm trying to download directly a pdf from a web app with the command

window.open(target, '_blank', 'location=no');

In Android it works great, but in iOS it only displays me a preview of the PDF, only a "DONE" button, without any "download" or "open with" action button.

Is there any way to handle it? i've read something but didn't find anything..

Thanks


Solution

  • iOS doesn't work same way as Android, it can't download files.

    Your options are:

    Open in Safari, this way it will be displayed and will have an "Open in" option that allows you to open it in another app. To open in safari use this code window.open(target, '_system');

    Download inside your app. Here you have to options, use XHR call or use cordova-plugin-file-transfer For XHR see my example on this answer Download binary data into the app sandbox using XHR2 request instead of cordova-file-transfer

    For file-transfer see this example code:

    // !! Assumes variable fileURL contains a valid URL to a path on the device,
    //    for example, cdvfile://localhost/persistent/path/to/downloads/
    
    var fileTransfer = new FileTransfer();
    var uri = encodeURI("http://some.server.com/yourFile.pdf");
    
    fileTransfer.download(
        uri,
        fileURL,
        function(entry) {
            console.log("download complete: " + entry.toURL());
        },
        function(error) {
            console.log("download error source " + error.source);
            console.log("download error target " + error.target);
            console.log("download error code" + error.code);
        },
        false,
        {}
    );