将此pdf保存在ionic上的缓存/本地存储中

时间:2016-03-21 12:04:13

标签: javascript cordova pdf ionic-framework pdfmake

对所有人来说。我按照本教程创建了一个模态视图,其中包含使用pdfmake生成的pdf。

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

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

2 个答案:

答案 0 :(得分:0)

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

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

git包含插件的完整文档,因此您可能需要的所有内容都应该存在。

答案 1 :(得分: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
        );
    };