FileTransfer Cordova download path

前端 未结 2 1861
隐瞒了意图╮
隐瞒了意图╮ 2020-12-10 21:57

I\'m using Cordova (5.4) to create apps for Android and Iphone. All goes fine, except I want to download images using the Cordova\'s plugin \"FileTransfer\" and I having som

相关标签:
2条回答
  • 2020-12-10 22:44

    @Luisma,

    Please find the sample code snippet to write pdf file in device using cordova file and file transfer plugin:

    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
        );
    };
    
    0 讨论(0)
  • 2020-12-10 22:51

    For paths into the application, I like to use

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

    This maps the different paths on every operative system, so its transparent to you, even through different SO or versions, it just pick the correct one.

    Happy coding!

    0 讨论(0)
提交回复
热议问题