2017-02-14 7 views
0

Ich arbeite an einer mobilen App mit Cordova. Da gibt es ein Tutorial im .docx Format in der App und ich muss es von der App an das Gerät weitergeben damit es geöffnet werden kann. Gibt es einen besseren Weg dies zu tun, lass es mich bitte wissen, aber hier ist was ich habe. Der Benutzer klickt auf eine Schaltfläche, um das Lernprogrammdoc anzuzeigen, das dann in der App geöffnet wird, die der Benutzer zum Anzeigen von DOCX-Dateien hat. Es funktioniert gut auf Android, aber nicht auf iOS und ich kann nicht herausfinden, warum. Ein FileTransferError wird in der Download-Funktion mit dem Fehlercode 3. auf iOS, aber nicht auf Android geworfen. Ich habe gesucht, aber alles, was ich finden kann, ist zum Herunterladen von einem Webserver. Kann mir jemand sagen, warum ich einen Fehlercode 3 auf iOS, aber nicht auf Android bekomme?Cordova Datei von App auf Gerät herunterladen

Dies ist der Code, der aufgerufen wird, wenn die Taste, um das Tutorial zu sehen geklickt wird:

function downloadTutorial() { 
    var userAgent = navigator.userAgent || navigator.vendor || window.opera; 
    if (userAgent.indexOf("Android") > -1) { 
     window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (directoryEntry) { 
      directoryEntry.getFile("app-tutorial.docx", { create: true }, function (fileEntry) { 

      download(fileEntry, encodeURI(cordova.file.applicationDirectory + "www/docs/app-tutorial-en.docx")); 
      }, function(e1){ 
       console.error(`get file failed`); 
       console.log(e1); 
      }); 
     }, function(e) { 
      console.error(`write failed`); 
      console.log(e); 
     }); 
    } 
    else if ((userAgent.indexOf("iPad") > -1 || userAgent.indexOf("iPhone") > -1 || userAgent.indexOf("iPod") > -1) && !window.MSStream) { 
     window.resolveLocalFileSystemURL(cordova.file.documentsDirectory, function (directoryEntry) { 
      console.log(directoryEntry); 
      directoryEntry.getFile("app-tutorial.docx", { create: true }, function (fileEntry) { 
       console.log(fileEntry); 

       download(fileEntry, encodeURI(cordova.file.applicationDirectory + "www/docs/app-tutorial-en.docx")); 
      }, function(e1){ 
       console.error(`get file failed`); 
       console.log(e1); 
      }); 
     }, function(e) { 
      console.error(`write failed`); 
      console.log(e); 
     }); 
    } 
} 

Und hier ist der Download-Funktion:

function download(fileEntry, uri) { 
    var fileTransfer = new FileTransfer(); 
    var fileURL = fileEntry.toURL(); 
    console.log(fileURL); 
    var options = new FileUploadOptions(); 
    options.headers = { Connection: "close" }; 

    fileTransfer.download(uri, encodeURI(fileURL), function (entry) { 
     console.log("Successful downloaded tutorial file."); 

     cordova.plugins.fileOpener2.open(
      fileURL, 
      "application/vnd.openxmlformats-officedocument.wordprocessingml.document", { 
       error : function(e) { 
        console.error(`Error status: ${e.status} - Error message: ${e.message}`); 
       }, 
       success : function() { 
        console.log('file opened successfully'); 
       } 
      } 
     ); 
    }, 
    function (error) { 
     console.error(`file transfer error ${error}`); // a FileTransferError is logged here with the source, destination and error code 3 
    }, false, options); 
} 

config.xml

<widget id="" version="0.18.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0"> 
<name></name> 
<description> 

</description> 
<author email="" href=""> 

</author> 
<content src="index.html" /> 
<plugin name="cordova-plugin-whitelist" spec="1" /> 
<plugin name="cordova-plugin-console" spec="1.0.3" /> 
<plugin name="cordova-plugin-file" spec="4.2.0"/> 
<plugin name="cordova-plugin-email" spec="1.1.1"/> 
<plugin name="cordova-sms-plugin"/> 
<plugin name="cordova-plugin-vibration"/> 
<plugin name="cordova-plugin-market"/> 
<plugin name="cordova-plugin-ble-central"/> 
<plugin name="cordova-sqlite-storage"/> 
<plugin name="cordova-plugin-globalization"/> 
<plugin name="cordova-plugin-file-transfer"/> 
<plugin name="cordova-plugin-file-opener2"/> 
<preference name="AndroidPersistentFileLocation" value="Compatibility"/> 
<preference name="AndroidExtraFilesystems" value="sdcard,cache,files-external"/> 
<preference name="iosExtraFileSystems" value="bundle,documents"/> 
<access origin="*" /> 
<preference name="Orientation" value="portrait" /> 
<allow-intent href="http://*/*" /> 
<allow-intent href="https://*/*" /> 
<allow-intent href="tel:*" /> 
<allow-intent href="sms:*" /> 
<allow-intent href="mailto:*" /> 
<allow-intent href="geo:*" /> 
<platform name="android"> 
    <allow-intent href="market:*" /> 
    <preference name="android-minSdkVersion" value="19"/> 
    <icon src="www/img/Icon-App.png"/> 
</platform> 
<platform name="ios"> 
    <allow-intent href="itms:*" /> 
    <allow-intent href="itms-apps:*" /> 
    <preference name="deployment-target" value="8.0"/> 
</platform> 
<engine name="android" spec="~4.3" /> 
<engine name="ios" spec="~4.1.1" /> 
<plugin name="cordova-sqlite-storage" spec="~1.4.8" /> 
</widget> 

Antwort

0

Ich konnte mit cordova-plugin-inappbrowser einen Workaround auf iOS finden. Die Verwendung von cordova.InAppBrowser.open("docs/app-tutorial-en.docx", "_blank", { location: "no", closebuttoncaption: "Done" }); funktioniert auf iOS, aber ich konnte es nicht mit Android arbeiten. Also verwende ich den Code in der Frage, da es für Android und den Code in dieser Antwort für iOS funktioniert. Wenn jemand eine bessere Lösung anbieten kann, bin ich für andere Vorschläge offen.

+1

closebuttoncaption funktioniert nur in iOS. Siehe http://stackoverflow.com/questions/42235892/cordova-download-file-from-app-to-device?rq=1 – user3417479

+0

Ja das ist wahr. Ich verwende dies nur auf iOS-Geräten. –

Verwandte Themen