From 67087e36ae59c6dc1b80c97cfcd1b9f3a4b03e28 Mon Sep 17 00:00:00 2001 From: schneefux Date: Sat, 22 Mar 2014 16:52:01 +0100 Subject: implementiere Download des PDFs --- js/index.js | 102 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 58 insertions(+), 44 deletions(-) (limited to 'js/index.js') diff --git a/js/index.js b/js/index.js index 8b5f6ca..b9c7242 100644 --- a/js/index.js +++ b/js/index.js @@ -16,6 +16,38 @@ * along with this program. If not, see . */ +function show_pdf(url){ + $('#input').attr('src', url); + + console.log("Download fertig, parse PDF"); + + var input = document.getElementById("input"); + var processor = document.getElementById("processor"); + var output = document.getElementById("output"); + + window.addEventListener("message", function(event){ + if(event.source != processor.contentWindow) return; + + switch(event.data){ + case "ready": + var xhr = new XMLHttpRequest; + xhr.open("GET", input.getAttribute("src"), true); + xhr.responseType = "arraybuffer"; + + xhr.onload = function(event){ + processor.contentWindow.postMessage(this.response, "*"); + }; + + xhr.send(); + break; + + default: + output.textContent = event.data.replace(/\s+/g, " "); + break; + } + }, true); +} + function fail(text){ alert("Etwas ist schiefgelaufen: " + text.toString()); } @@ -23,50 +55,32 @@ function fail(text){ function download_pdf(link){ var filename = link.substring(link.lastIndexOf('/') + 1); - window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){ - fileSystem.root.getFile(filename, {create: true, exclusive: false}, function(fileEntry){ - console.log("Bereite Download vor"); - var localPath = fileEntry.fullPath; - if(/*device.platform == "Android" && /* TODO fehleranfällig? */localPath.indexOf("file://") == 0){ - localPath = localPath.substring(7); - } - var ft = new FileTransfer(); - console.log("Bereit für Download " + ft); - ft.download(link, - localPath, function(entry){ - console.log("foo"); - $('#input').attr('src', entry.fullPath); - - console.log("Download fertig, parse PDF"); - - var input = document.getElementById("input"); - var processor = document.getElementById("processor"); - var output = document.getElementById("output"); - - window.addEventListener("message", function(event){ - if(event.source != processor.contentWindow) return; - - switch(event.data){ - case "ready": - var xhr = new XMLHttpRequest; - xhr.open("GET", input.getAttribute("src"), true); - xhr.responseType = "arraybuffer"; - - xhr.onload = function(event){ - processor.contentWindow.postMessage(this.response, "*"); - }; - - xhr.send(); - break; - - default: - output.textContent = event.data.replace(/\s+/g, " "); - break; - } - }, true); - }, fail); - }, fail); - }, fail); + window.requestFileSystem( + LocalFileSystem.PERSISTENT, 0, + function onFileSystemSuccess(fileSystem){ + fileSystem.root.getFile( + filename, {create: true, exclusive: false}, + function gotFileEntry(fileEntry){ + var sPath = "/storage/sdcard0" + fileEntry.fullPath; // !!! TODO Unterstützung anderer Geräte + console.log("Pfad: " + sPath); + var fileTransfer = new FileTransfer(); + fileEntry.remove(); + + fileTransfer.download( + link, + sPath, + function(theFile){ + console.log("Download fertig: " + theFile.toURL()); + show_pdf(theFile.toURL()); + }, + function(error){ + console.log("Fehler: " + error.source + " - "+ error.target + " - " + error.code); + } + ); + }, + fail); + }, + fail); } function get_plan(link){ -- cgit v1.3.1