0

对大家好。我按照本教程使用 pdfmake 生成的 pdf 创建模态视图。

http://gonehybrid.com/how-to-create-and-display-a-pdf-file-in-your-ionic-app/

我的简单问题是如何将 pdf 保存在缓存中的本地存储中?我需要它通过电子邮件发送 pdf 或使用 openfile2 打开它。我正在使用离子和科尔多瓦。

4

2 回答 2

0

我不知道你是如何编码的,但我知道你应该使用什么插件:

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

git 包含插件的完整文档,因此您可能需要的一切都应该在那里。

于 2016-03-21T12:22:20.593 回答
0

使用 cordova 文件和文件传输插件在设备中写入 pdf 文件的示例代码:

var fileTransfer = new FileTransfer();

    if (sessionStorage.platform.toLowerCase() == "android") {
        window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, onFileSystemSuccess, onError);
    } else {
        // for iOS
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
    }

    function onError(e) {
        navigator.notification.alert("Error : Downloading Failed");
    };

    function onFileSystemSuccess(fileSystem) {
        var entry = "";
        if (sessionStorage.platform.toLowerCase() == "android") {
            entry = fileSystem;
        } else {
            entry = fileSystem.root;
        }
        entry.getDirectory("Cordova", {
            create: true,
            exclusive: false
        }, onGetDirectorySuccess, onGetDirectoryFail);
    };

    function onGetDirectorySuccess(dir) {
        cdr = dir;
        dir.getFile(filename, {
            create: true,
            exclusive: false
        }, gotFileEntry, errorHandler);
    };

    function gotFileEntry(fileEntry) {
        // URL in which the pdf is available
        var documentUrl = "http://localhost:8080/testapp/test.pdf";
        var uri = encodeURI(documentUrl);
        fileTransfer.download(uri, cdr.nativeURL + "test.pdf",
            function(entry) {
                // Logic to open file using file opener plugin
            },
            function(error) {
                navigator.notification.alert(ajaxErrorMsg);
            },
            false
        );
    };
于 2016-03-21T14:36:23.020 回答