如何在Cordova应用程序中从字节数组下载任何文件? [英] How to download any file from Byte Array in Cordova application?

查看:51
本文介绍了如何在Cordova应用程序中从字节数组下载任何文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有cordova-离子应用程序。
我想从Web服务下载文件,该文件可以是任何类型(JPG,PDG,DOCX等)。我无法从直接URL下载文件。因此,该应用正在从Web服务获取文件的字节数组。

I have a cordova - ionic application. I want to download a file from webservice and the file may be any type(JPG,PDG,DOCX etc). I cannot download the file from direct URL. So the app is taking byte array of the file from Webservice.

任何人都知道如何从字节数组下载Mobile中的文件。

Anybody know how to download the file in Mobile from the Byte Array. Please help me.

推荐答案

您可以使用
cordova-plugin-file和
cordova- plugin-file-opener2插件。

you can use the cordova-plugin-file and the cordova-plugin-file-opener2 plugin.

https ://github.com/apache/cordova-plugin-file

https://github.com/pwlin/cordova-plugin-file-opener2

使用网络服务,您的代码应如下所示:

in function with the webservice your code should look like that:

var bytes = new Uint8Array(data.d);
app.writePDFToFile(fileName.split(fileName.split, bytes);

是强制下载的函数:

writePDFToFile: function (fileName, data) {

try {
    window.resolveLocalFileSystemURL(cordova.file.externalApplicationStorageDirectory, function (directoryEntry) {
        directoryEntry.getFile(fileName, { create: true }, function (fileEntry) {

            fileEntry.createWriter(function (fileWriter) {
                fileWriter.onwriteend = function (e) {

                    //window.open(cordova.file.externalApplicationStorageDirectory + fileName, '_system', 'location=yes');

                    cordova.plugins.fileOpener2.open(cordova.file.externalApplicationStorageDirectory + fileName, 'application/pdf',
                        {
                            error: function (e) {
                                console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
                            },
                            success: function () {
                                console.log('file opened successfully');
                            }
                        }
                    );
                };

                fileWriter.onerror = function (e) {
                    alert(e);
                };

                var blob = new Blob([data], { type: 'application/pdf' });

                fileWriter.write(blob);

            }, function onerror(e) {
                alert(e);
            });
        }, function onerror(e) {

            alert(e);
        });
    }, function onerror(e) {            
        alert(e);
    });

} catch (e) {
     alert(e);
}
},

希望这会有所帮助!

这篇关于如何在Cordova应用程序中从字节数组下载任何文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆