繁体   English   中英

Cordova 6.3.1 下载 PDF 并使用 FileSystem、FileTransfer 和 FileOpener2 打开

[英]Cordova 6.3.1 download PDF and open with FileSystem, FileTransfer and FileOpener2

我的头撞在我的桌子上,因为我似乎没有找到任何有效的答案。

我想将 PDF 下载到 Android 设备的本地存储,然后在外部阅读器中打开它,因为 Android 无法在浏览器中显示 PDF。 对于 iOS,我只使用 InAppBrowser 插件,顺便说一句,效果很好。 我使用科尔多瓦 6.3.1。

所以,这是我的代码:

if (cordova.platformId === "android") {
            var remoteFile = url;
            var localFileName = "tmp.pdf";

            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {

                //var fileSystemRoot = cordova.file.dataDirectory; Does not work...
                var fileSystemRoot = fileSystem.root.toURL()

                console.log(cordova.file.dataDirectory);

                    var ft = new FileTransfer();
                    ft.download(remoteFile,
                    fileSystemRoot + "tmp.pdf", function(entry) {
                        cordova.plugins.fileOpener2.open(
                            entry.toURL(),
                            'application/pdf',
                            {
                                error : function(e) {
                                    console.log('Error status: ' + e.status + ' - Error message: ' + e.message + ' - URL: ' + messageObj.url);
                            },
                                success : function () {
                                    console.log('file opened successfully');
                                    console.log(fileSystemRoot);
                                    console.log(entry.toURL());
                                }
                            }
                        );
                    }, function(error) {
                        console.log("Error in downloading");
                        console.log(error);
                    });
            }, function(error) {
                console.log("Error in requesting filesystem");
                console.log(error);
            });
}

我尝试了很多不同的东西。 fileSystem.root.fullpath, fileSystem.root.toURL(), fileSystem.root.nativeURL 但我总是得到一个似乎与设备不对应的路径。 我总是收到下载成功的成功消息,adobe reader 弹出但说文件不可读。 对我来说并不奇怪,因为它给我的路径是这样的:

file:///data/data/ch.novalogix.novalib/files/files

那不可能是真的? 我在整个系统中搜索了上传的文件,但我认为它没有下载。 我想我总是走错路......

有什么想法吗? 提前致谢!

我使用固定路径而不是 localFileSystem。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM