首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >fileOpener2有什么问题?

fileOpener2有什么问题?
EN

Stack Overflow用户
提问于 2016-07-26 22:23:48
回答 3查看 1.7K关注 0票数 0

这个问题是为了更好的隐藏:我正在尝试使用cordova fileOpener2插件来打开我的应用程序资产中的pdf文件。由于下面代码的第14行和后续代码,我获得了文件的uri。uri然后作为变量(nonencodeduri)存储。但是,当我试图在代码的第二部分中使用变量时,FileOpener2需要文件的路径(从第58行),它会停止。这是令人惊讶的,因为如果我编写硬编码到同一文件行58 (var uri = var uri =encodeURI(“在应用程序资产中的文件路径”)),它可以工作。谢谢你帮我解决这个问题。

这是完整的代码(学分:甘地)

代码语言:javascript
复制
var  entry, documentname, documentid, referenceID, callLogID, filePath, blob,cdr,fileObject;
var filename = "test.pdf";


$(document).ready(function() {
    document.addEventListener("deviceready", onDeviceReady, false);
});

var fileURL = "";
var imagePath = "";
function onDeviceReady() {  
    sessionStorage.platform = device.platform;
    var fileTransfer = new FileTransfer();
     $('a[href$=\\.pdf]').click(function()
        {   
            try {
                alert('Hi boys');   
                var urinonencoded = this.href;
                alert(urinonencoded + ' and voila');                                        
                if (sessionStorage.platform.toLowerCase() == "android") {
                    window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory,onFileSystemSuccess, onError);
                }
                else {
                    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,onFileSystemSuccess, onError);
                }

            }
            catch(err) {
                alert("ER - " + err.message);
            }

        }); 

        function onError(e) {
            alert("onError");
        };

        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) {
            dir.getDirectory("Sample_App", {create: true, exclusive: false}, onGetDirectorySuccess1, onGetDirectoryFail);
        };
        function onGetDirectorySuccess1(dir) {
            cdr = dir;
            dir.getFile(filename,{create:true, exclusive:false},gotFileEntry, errorHandler);
        };
        function gotFileEntry(fileEntry) {                                      
                /*var uri = encodeURI(myref);*/

                    var uri = urinonencoded;
                    alert (uri);


                alert("dest - " + cdr.nativeURL+filename);
                fileTransfer.download(uri,cdr.nativeURL+filename,
                    function(entry) {                       
                        openFile();
                    },
                    function(error) {
                        alert("download error source " + error.source);
                        alert("download error target " + error.target);
                        alert("upload error code" + error.code);
                        alert("error");
                    },
                    true);              
        };

        function openFile() {
            alert("URL - " + cdr.nativeURL+filename);
            cordova.plugins.fileOpener2.open(
                cdr.nativeURL+filename, 
                'application/pdf', 
                //'text/plain',
                { 
                    error : function(e) { 
                        alert('Error status: ' + e.status + ' - Error message: ' + e.message);
                    },
                    success : function () {                                    
                    }
                }
            );
        };
        function onFileSystemSuccessDelete(fileSystem) {
            var entry="";
            if (sessionStorage.platform.toLowerCase() == "android") {
                entry=fileSystem;
            }
            else {
                entry=fileSystem.root;
            }   
            entry.getDirectory("Cordova/Sample_App", {create: true, exclusive: false}, onGetDirectorySuccessDelete, onGetDirectoryFail);

        };
        function onGetDirectorySuccessDelete(dir) {
            dir.getFile(filename,{create: true, exclusive:false},gotFileEntryDelete, fail);                     
        };

        function gotFileEntryDelete(fileEntry) {
            fileEntry.remove();
            var uri = encodeURI("http://SERVER_IP:PORT/test.pdf");
                fileTransfer.download(uri,cdr.nativeURL+filename,
                    function(entry) {
                        console.log("download complete: " + entry.toURL());                     
                        openFile();
                    },
                    function(error) {
                        alert("download error source " + error.source);
                        alert("download error target " + error.target);
                        alert("upload error code" + error.code);
                        alert("error");
                    },
                    true);              
        };      

        function fail(error){
            alert("ec - " + error.code);
        };

        function  errorHandler(e) {
            var msg = '';
            switch (e.code) {
                case FileError.QUOTA_EXCEEDED_ERR:
                    msg = 'QUOTA_EXCEEDED_ERR';
                    break;
                case FileError.NOT_FOUND_ERR:
                    msg = 'NOT_FOUND_ERR';
                    break;
                case FileError.SECURITY_ERR:
                    msg = 'SECURITY_ERR';
                    break;
                case FileError.INVALID_MODIFICATION_ERR:
                    msg = 'INVALID_MODIFICATION_ERR';
                    break;
                case FileError.INVALID_STATE_ERR:
                    msg = 'INVALID_STATE_ERR';
                    break;
                default:
                    msg = e.code;
                    break;
            };
            alert("Msg - " + msg);
        };

        function onGetDirectoryFail(error) {
            alert("onGetDirectoryFail");
        };

        $('#delete').click(ClearDirectory);

        function ClearDirectory() {
            alert("delete");
            if (sessionStorage.platform.toLowerCase() == "android") {
                window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory,onFileSystemDirSuccess, fail);
            }
            else {
                window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,onFileSystemDirSuccess, fail);
            }        
        }
        function onFileSystemDirSuccess(fileSystem) {
            var entry = "";
            if (sessionStorage.platform.toLowerCase() == "android") {
                entry=fileSystem;
            }
            else {
                entry=fileSystem.root;
            }   
            entry.getDirectory("Cordova",{create : true, exclusive : false},
                function(entry) {
                    entry.removeRecursively(function() {
                        console.log("Remove Recursively Succeeded");
                    }, fail);
                }, getDirFail);
        }

        function getDirFail(error){
            alert("getDirFail - " + error.code);
        };

}

我用:

代码语言:javascript
复制
<script>
    $('a[href$=\\.pdf]').click(function() {
    var myuri = this.href ;   
        alert(this.href);

  /*alert just to make sure I got the right uri (which works fine)*/

    cordova.plugins.fileOpener2.open(
        'this.href', // You can also use a Cordova-style file uri: cdvfile://localhost/persistent/Download/starwars.pdf
        'application/pdf', 
        { 
                error : function(e) 
             { 
                alert('Error status: ' + e.status + ' - Error message: ' + e.message);
            },
            success : function () 
            {
                alert('file opened successfully');                
            }
        }
    );
        return false; 
    });

    </script>

它挂起(我在config.xml文件中声明了插件,并显示在资产中)。

你能找出我的错误吗?

非常感谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-07-27 04:37:29

基本上,问题在于您传递给fileopener的开放函数的file path参数的方式,而插件本身并没有什么问题。

如果在单引号(如this )中传递'this.href'对象,它将被视为一个普通字符串,这就是问题所在。因此,您可以使用Joerg在他的回答中所建议的方法。

请查看这个演示Cordova文件操作的基本工作样例应用程序。这应该能帮你开始工作。

票数 1
EN

Stack Overflow用户

发布于 2016-07-27 02:20:44

“this.href”是错误的,试着这样做:

代码语言:javascript
复制
$('a[href$=\\.pdf]').click(function () {
    var myuri = this.href;
    alert(this.href);

    /*alert just to make sure I got the right uri (which works fine)*/

    cordova.plugins.fileOpener2.open(
        myuri, // You can also use a Cordova-style file uri: cdvfile://localhost/persistent/Download/starwars.pdf
        'application/pdf',
        {
            error: function (e) {
                alert('Error status: ' + e.status + ' - Error message: ' + e.message);
            },
            success: function () {
                alert('file opened successfully');
            }
        }
    );
    return false;
});
票数 1
EN

Stack Overflow用户

发布于 2016-08-08 07:20:29

总结一下这个复杂问题的答案:first:fileopener2将无法独立工作,它需要其他插件来满足在android上打开pdf文件的目的。其他插件需要在甘地的优秀演示应用程序可用的这里中提到。第二:使用甘地的app.js文件是必要的,它结合了对插件的不同调用。第三文件中涉及到许多函数。在它的路径是硬编码,所以一切似乎运行良好。但是,如果您必须使用动态路径,那么它必须是全局声明的(换句话说,不要使用"var“,而只使用myvar = this.href。

虽然我自己找到了最后的部分,但所有的功劳都归功于甘地出色的演示应用程序,这是他在github上为新插件用户提供的一个宝库。+1 :-)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38600825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档