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

Save this pdf in the cache/local storage on ionic

本文关键字:存储 缓存 保存 pdf 存在 ionic 将此      更新时间:2023-09-26

Ho 给大家。我按照本教程创建了一个模态视图,其中包含使用 pdfmake 生成的 pdf。

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

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

我不知道

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

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

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

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