2017-02-14 8 views
0

私はCordovaを使用してモバイルアプリを開発中です。そこにアプリケーションの.docx形式のチュートリアルがあり、私はそれを開くことができるように、アプリからデバイスに渡す必要があります。これを行うより良い方法がありますか私に教えてくださいが、ここに私が持っているものがあります。ユーザーは、ボタンをクリックしてチュートリアルのドキュメントを表示します。このチュートリアルは、ユーザーが持っている.docxファイルを表示できるアプリケーションで開きます。 Android上ではうまく動作しますが、iOSでは動作しません。その理由を把握することはできません。 FileTransferErrorはダウンロード機能で、iOSではエラーコード3でスローされますが、Androidではスローされません。私は検索しましたが、私が見つけることができるものはすべてWebサーバーからダウンロードするものです。誰かが私がiOS上でエラーコード3を得る理由を教えてもらえますか?Cordovaファイルをダウンロードするアプリからデバイス

これは、チュートリアルを表示するには、ボタンがクリックされたときに呼び出されるコードです:

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); 
     }); 
    } 
} 

そしてここでは、ダウンロード機能である:

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> 

答えて

0

私はcordova-plugin-inappbrowserを使用してiOSで回避策を見つけることができました。 cordova.InAppBrowser.open("docs/app-tutorial-en.docx", "_blank", { location: "no", closebuttoncaption: "Done" });を使用するとiOSで動作しますが、Androidで動作させることができませんでした。だから私はAndroidとiOSのこの答えのコードで動作するので、質問のコードを使用しています。誰かがより良い解決策を提供できるなら、私は他の提案にもオープンしています。

+1

closebuttoncaptionはiOSでのみ動作します。ここをクリックhttp://stackoverflow.com/questions/42235892/cordova-download-file-from-app-to-device?rq=1 – user3417479

+0

はい、それは本当です。私はiOSデバイスでのみこれを使用しています。 –

関連する問題