【问题标题】:Cordova 6.3.1 download PDF and open with FileSystem, FileTransfer and FileOpener2Cordova 6.3.1 下载 PDF 并使用 FileSystem、FileTransfer 和 FileOpener2 打开
【发布时间】:2016-10-04 16:29:12
【问题描述】:

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

我想将 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

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

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

【问题讨论】:

    标签: android ios cordova pdf


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-07-30
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      相关资源
      最近更新 更多