diff options
| -rw-r--r-- | config.xml | 11 | ||||
| -rw-r--r-- | js/index.js | 98 |
2 files changed, 67 insertions, 42 deletions
@@ -20,6 +20,16 @@ The "device" permission is required for the 'deviceready' event. --> <feature name="http://api.phonegap.com/1.0/device" /> + <feature name="http://api.phonegap.com/1.0/file" /> + <feature name="File"> + <param name="android-package" value="org.apache.cordova.file.FileUtils" /> + </feature> + <feature name="FileTransfer"> + <param name="android-package" value="org.apache.cordova.filetransfer.FileTransfer" /> + </feature> + <feature name="Storage"> + <param value="org.apache.cordova.Storage" name="android-package" /> + </feature> <!-- If you do not want any permissions to be added to your app, add the @@ -51,6 +61,7 @@ A list of available plugins are available at https://build.phonegap.com/docs/plugins --> <gap:plugin name="org.apache.cordova.file-transfer" /> + <gap:plugin name="org.apache.cordova.file" /> <!-- Define app icon for each platform. --> <icon src="icon.png" /> diff --git a/js/index.js b/js/index.js index 8b5f6ca..b9c7242 100644 --- a/js/index.js +++ b/js/index.js @@ -16,57 +16,71 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -function fail(text){ - alert("Etwas ist schiefgelaufen: " + text.toString()); -} +function show_pdf(url){ + $('#input').attr('src', url); -function download_pdf(link){ - var filename = link.substring(link.lastIndexOf('/') + 1); + 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; - 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); + switch(event.data){ + case "ready": + var xhr = new XMLHttpRequest; + xhr.open("GET", input.getAttribute("src"), true); + xhr.responseType = "arraybuffer"; - console.log("Download fertig, parse PDF"); + xhr.onload = function(event){ + processor.contentWindow.postMessage(this.response, "*"); + }; - var input = document.getElementById("input"); - var processor = document.getElementById("processor"); - var output = document.getElementById("output"); + xhr.send(); + break; - window.addEventListener("message", function(event){ - if(event.source != processor.contentWindow) return; + default: + output.textContent = event.data.replace(/\s+/g, " "); + break; + } + }, true); +} - switch(event.data){ - case "ready": - var xhr = new XMLHttpRequest; - xhr.open("GET", input.getAttribute("src"), true); - xhr.responseType = "arraybuffer"; +function fail(text){ + alert("Etwas ist schiefgelaufen: " + text.toString()); +} - xhr.onload = function(event){ - processor.contentWindow.postMessage(this.response, "*"); - }; +function download_pdf(link){ + var filename = link.substring(link.lastIndexOf('/') + 1); - xhr.send(); - break; + 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(); - default: - output.textContent = event.data.replace(/\s+/g, " "); - break; - } - }, true); - }, fail); - }, fail); - }, fail); + 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){ |
