Phonegap代码获取要在html中发布的文件的完整路径

phonegap code get the full path of a file to post in html

本文关键字:文件 路径 获取 代码 html Phonegap      更新时间:2023-09-26

我正在为离线图书应用程序制作一个phonegap应用程序。在这里,我将一些文件保存在设备根目录的文件夹中,并显示该文件夹中的所有文件供用户选择。当用户点击一个文件来加载它,我采取文件的完整路径,并给予另一个javascript函数来加载它。

文件保存在手机根目录下的myappfolder中,我正在获取文件路径。我的代码如下:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function(fileSystem) {    
    fileSystem.root.getDirectory("myappfolder",
    {
        create: true
    }, function(directory) {
        var directoryReader = directory.createReader();
        directoryReader.readEntries(function(entries) {             
        if(entries.length > 0 ) {
            for (i=0; i<entries.length; i++) {
                var name = entries[i].name;
                var div1 = document.createElement('div');
                div1.className = 'books';
                div1.id = entries[i].fullPath;
                div1.innerHTML =  entries[i].name +'<hr>';
                document.getElementById('content'). appendChild(div1);
                div1.onclick =function (){
                    redirect(this.id);
                }
            }           
        }

当我使用这段代码加载文件时,我得到

network error in file://myappfolder/file.jpg

请帮助我解决这个问题,因为我是新的phonegap。

我将此文件传递给另一个js函数,该函数将以翻页书格式显示此文件,如果我在app文件夹

中给出静态文件,则对我来说工作很好

经过一番研究,我找到了解决方案:

fullPath只给出根目录的路径,但是如果你给

条目[我].toNativeUrl

给出了文件的完整路径,上面代码的结果是

文件:///存储/模拟/0/myappfolder/name.pdf。

这个结果来自我的手机(三星galaxy s4)

在iPhone上也能正常工作。