2017-02-14 57 views
0

我正在使用科尔多瓦的移动应用程序。它有一个在.docx格式的应用程序教程,我需要从应用程序传递给设备,以便它可以打开。请问是否有更好的方法让我知道,但这里是我的。用户点击一个按钮来查看教程文档,然后在用户可以查看.docx文件的任何应用程序中打开该教程文档。它在Android上运行得很好,但在iOS上却没有,我不知道为什么。下载函数中出现FileTransferError,错误代码为3.在iOS上,但不在Android上。我已经搜索过,但我可以找到的所有内容都是从Web服务器下载的。任何人都可以告诉我为什么我在iOS上得到错误代码3,但不是Android?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

我能够在iOS上使用cordova-plugin-inappbrowser找到解决方法。使用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设备上使用它。 –

相关问题