简体   繁体   English

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

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

Im banging my head against my desk because I dont seem to find any answer that works.我的头撞在我的桌子上,因为我似乎没有找到任何有效的答案。

I want to download a PDF to the local storage of an android device, and then open it in an external reader as Android is not able to display PDF in the browser.我想将 PDF 下载到 Android 设备的本地存储,然后在外部阅读器中打开它,因为 Android 无法在浏览器中显示 PDF。 For iOS I simply use the InAppBrowser plugin which works nice btw.对于 iOS,我只使用 InAppBrowser 插件,顺便说一句,效果很好。 Im using cordova 6.3.1.我使用科尔多瓦 6.3.1。

So, this is my code:所以,这是我的代码:

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

I have tried tons of different things.我尝试了很多不同的东西。 fileSystem.root.fullpath, fileSystem.root.toURL(), fileSystem.root.nativeURL but I always end up with a path that does not seem to correspond with the device. fileSystem.root.fullpath, fileSystem.root.toURL(), fileSystem.root.nativeURL 但我总是得到一个似乎与设备不对应的路径。 I always get the success message that the download worked, adobe reader pops up but says the file is not readable.我总是收到下载成功的成功消息,adobe reader 弹出但说文件不可读。 Not a surprise to me as the path it gives me is something like:对我来说并不奇怪,因为它给我的路径是这样的:

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

That can simply not be true?那不可能是真的? I searched the whole system for the uploaded file but I dont think its downloaded.我在整个系统中搜索了上传的文件,但我认为它没有下载。 I guess I always get a wrong path...我想我总是走错路......

Any ideas?有什么想法吗? Thanks in advance!提前致谢!

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

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

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