简体   繁体   English

Cordova将文件从App下载到设备

[英]Cordova Download File From App to Device

I am working on a mobile app using Cordova. 我正在使用Cordova开发移动应用程序。 There it a tutorial in .docx format in the app and I need to pass it from the app to the device so it can be opened. 在应用程序中有.docx格式的教程,我需要将其从应用程序传递到设备,以便可以将其打开。 Is there is a better way to do this let me know please but here is what I have. 有没有更好的方法可以做到这一点,请告诉我,但这就是我所拥有的。 The user clicks a button to view the tutorial doc which then opens in whatever app the user has that can view .docx files. 用户单击一个按钮以查看教程文档,然后在用户可以查看.docx文件的任何应用程序中打开该文档。 It works just fine on Android but not on iOS and I can't figure out why. 它只能在Android上正常运行,而不能在iOS上运行,我不知道为什么。 A FileTransferError is thrown in the download function with error code 3. on iOS but not Android. 在iOS上但不是Android上,下载功能中抛出FileTransferError,错误代码为3。 I have searched around but everything I can find is for downloading from a web server. 我已经搜索过了,但是我能找到的所有东西都是从Web服务器下载的。 Can anyone tell me why I get an error code 3 on iOS but not Android? 谁能告诉我为什么我在iOS上却收到错误代码3,但在Android上却没有?

This is the code that is called when the button to view the tutorial is clicked: 这是单击查看教程的按钮时调用的代码:

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

And here is the download function: 这是下载功能:

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 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>

I was able to find a workaround on iOS using cordova-plugin-inappbrowser. 我能够使用cordova-plugin-inappbrowser在iOS上找到解决方法。 Using cordova.InAppBrowser.open("docs/app-tutorial-en.docx", "_blank", { location: "no", closebuttoncaption: "Done" }); 使用cordova.InAppBrowser.open("docs/app-tutorial-en.docx", "_blank", { location: "no", closebuttoncaption: "Done" }); works on iOS but I couldn't get it to work on Android. 在iOS上可以使用,但在Android上却无法使用。 So I'm using the code in the question since it works for Android and the code in this answer for iOS. 因此,我在问题中使用代码,因为它适用于Android,而此答案中的代码适用于iOS。 If anyone can provide a better solution I'm open to other suggestions. 如果有人可以提供更好的解决方案,我欢迎其他建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 下载文件并在下载应用程序或cordova应用程序的通知中显示 - Download a file and show it in downloads app or in notification in a cordova app 使用javascript将文件从服务器下载到移动设备 - download a file from server to mobile device in javascript 使用 jquery 从本地设备下载文件 - download file from local device with jquery 如何从Cordova应用程序中的Byte Array下载任何文件? - How to download any file from Byte Array in Cordova application? 在Web中将目录的递归副本复制到Cordova中的设备文件系统 - Recursive copy of directory from web to device file system in Cordova 使用 XHR2 请求而不是cordova-file-transfer 将二进制数据下载到应用程序沙箱中 - Download binary data into the app sandbox using XHR2 request instead of cordova-file-transfer 如何在javascript中单击按钮在cordova移动应用程序中创建和下载csv文件 - How to create and download csv file in cordova mobile app with button click in javascript 从 Cordova iOS 应用程序从网站下载文件 - Downloading a file from a website from a Cordova iOS app 从 Angular 应用程序中的资产下载文件 - download a file from assets in Angular app Chrome APP/Extension 从 URL 下载文件 - Chrome APP/Extension download file from URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM