Download PDFs in Cordova InAppBrowser (3.1.0)

My result is now, that inAppBrowser by default isn't capable of handling Downloads at all and doesn't even process links with a file suffix like ".pdf".

a) So one workaround is to open the file in Google Docs by rewriting the link. This works because it's no more a PDF Document, but an external link:
https://docs.google.com/viewer?url=https://www.example.com/yourOnlinePdfDocument.pdf

b) Use the "beforeload" event to decide which browser to use before the link is further processed:

  1. var app = {
  2.   [...]
  3.   _launchWebApp: function () {
  4.     // Set global variables:
  5.     //    previousUrl = null;
  6.     // Open URL in inappbrowser:
  7.     var webAppWindow = cordova.InAppBrowser.open(this.config.webAppUrl, '_blank', 'location=no,beforeload=get,zoom=no,toolbar=no,enableViewportScale=yes');
  8.     // Register events:
  9.     webAppWindow.addEventListener('beforeload', this.onBeforeLoad.bind(this, webAppWindow), false);
  10.   },
  11.   /**
  12.    *
  13.    */
  14.   onBeforeLoad: function(webAppWindow, params, callback){
  15.     // Special handling of downloads. InAppBrowser doesn't handle them
  16.     // so we have to manually preprocess and open in system browser:
  17.     if(params.url.match(".pdf")){
  18.       cordova.InAppBrowser.open(params.url, '_system');
  19.     } else {
  20.       // Default handling:
  21.       callback(params.url);
  22.     }
  23.   },

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.

Weitere Informationen über Formatierungsoptionen