Filesystems Cordova FileEntry.file()返回大小0

Filesystems Cordova FileEntry.file()返回大小0,filesystems,filereader,Filesystems,Filereader,我遇到了一个问题,当我在windows上将jpg保存为八位字节流时,后续读取返回一个大小为0的文件。我可以在硬盘上看到文件,文件看起来很好,但读取文件失败 下面的代码是我用来与文件系统进行交互的代码,其中加入了一些黑客来试图解决这个问题 function downloadFileFromUrl(url, localPath, contentType, successCallback, failureCallback) { var xhr = new XMLHttpRequest();

我遇到了一个问题,当我在windows上将jpg保存为八位字节流时,后续读取返回一个大小为0的文件。我可以在硬盘上看到文件,文件看起来很好,但读取文件失败

下面的代码是我用来与文件系统进行交互的代码,其中加入了一些黑客来试图解决这个问题

function downloadFileFromUrl(url, localPath, contentType, successCallback, failureCallback) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'blob';

    xhr.onload = function () {
        if (this.status == 200) {
            localPath = encodeURI(localPath);
            writePersistantFile(localPath, this.response, true, contentType,
                function (fileEntry) {
                    if (successCallback != undefined) {
                        successCallback(fileEntry);
                    }
                },
                function (error) {
                    if (failureCallback != undefined) {
                        failureCallback(error);
                    }
                });
        }
    };
    xhr.send();
}

function createDirectoryRecursively(path, parentDir, callback, errorCallback) {
    if (path == null || path.length == 0) {
        callback(parentDir);
        return;
    }
    var index = path.indexOf('/');
    var dirName = null;
    var remainder = null;
    if (index <= 0) {
        dirName = path;
    } else {
        dirName = path.substring(0, index);
        remainder = path.substring(index + 1);
    }

    parentDir.getDirectory(dirName, { create: true, exclusive: true }, function (dirEntry) {
        self.createDirectoryRecursively(remainder, dirEntry, callback, errorCallback);
    }, function (error) {
        if (error.code == 12) {
            parentDir.getDirectory(dirName, { create: false }, function (dirEntry) {
                self.createDirectoryRecursively(remainder, dirEntry, callback, errorCallback);
            }, function (error2) {
                errorCallback(error);
            });
            return;
        }
        errorCallback(error);
    });
}

function getFile(fileName, parentDir, callback, errorCallback) {
    if (parentDir == null) {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
            function (fs) {
                getFile(fileName, fs.root, callback, errorCallback);
            },
            function (error) {

            });
        return;
    }
    if (fileName.indexOf('/') >= 0) {
        var dirName = fileName.substring(0, fileName.lastIndexOf('/'));
        createDirectoryRecursively(dirName, parentDir,
            function (directory) {
                var name = fileName.substring(fileName.lastIndexOf('/') + 1);
                getFile(name, directory, callback, errorCallback);
            }, errorCallback);
        return;
    }
    parentDir.getFile(fileName, { create: true },
        function (fileEntry) {
            console.log('obtained file');
            callback(fileEntry);
        },
        function (error) {
            if (error.code == 1) {
                parentDir.getFile(fileName, { create: false },
                    function (fileEntry) {
                        console.log('obtained file');
                        callback(fileEntry);
                    },
                    function (error) {
                        if (error.code == 12) {
                            return;
                        }
                        errorCallback(error);
                    });
                return;
            }
            errorCallback(error);
        });
}

function readPersistantFile(filePath, successCallback, errorCallback) {
    getFile(filePath, null,
        function (fileEntry) {
            console.log('obtained file');
            fileEntry.file(function (file) {
                console.log('file size: ' + file.size);
                var reader = new FileReader();

                reader.onloadend = function () {
                    console.log("Successful file read");
                    if (successCallback != undefined) {
                        successCallback(this.result);
                    }
                };

                var extension = filePath.substring(filePath.lastIndexOf('.') + 1);
                if (extension == 'json') {
                    reader.readAsText(file);
                } else {
                    reader.readAsDataURL(file);
                }
            });

        },
        function (error) {
            console.log('An error occured while getting the file');
            console.error(error);
            if (errorCallback != undefined) {
                errorCallback(error);
            }
        });
}

function writePersistantFile(filePath, data, isBinary, contentType, successCallback, errorCallback) {
    var dataSize = (data.length != undefined) ? data.length : (data.size != undefined)? data.size : 5 * 1024 * 1024;
    window.requestFileSystem(LocalFileSystem.PERSISTENT, dataSize, function (fs) {
        console.log('file system opened: ' + fs.name);

        var lastIndex = filePath.lastIndexOf('/');
        var directory = filePath.substring(0, lastIndex);
        createDirectoryRecursively(directory, fs.root, function (parentDir) {
            console.log('Directory created');
            var fileName = filePath.substring(lastIndex + 1);
            getFile(fileName, parentDir,
                function (fileEntry) {
                    console.log('obtained file');
                    fileEntry.createWriter(function (fileWriter) {
                        console.log('created writer');
                        if (typeof (data) == 'string') {
                            fileWriter.write(new Blob([data], { type: contentType }));
                        } else {
                            fileWriter.write(data);
                        }

                        if (successCallback != undefined) {
                            successCallback(fileEntry);
                        }
                    });
                },
                function (error) {
                    console.log('An error occured while getting the file');
                    console.error(error);
                    if (errorCallback != undefined) {
                        errorCallback(error);
                    }
                });

        }, function (error) {
            console.log('An error occured while saving the file');
            console.error(error);
            if (errorCallback != undefined) {
                errorCallback(error);
            }
        });
    });
}
函数downloadFileFromUrl(url、localPath、contentType、successCallback、failureCallback){
var xhr=new XMLHttpRequest();
xhr.open('GET',url,true);
xhr.responseType='blob';
xhr.onload=函数(){
如果(this.status==200){
localPath=encodeURI(localPath);
writePersistantFile(localPath、this.response、true、contentType、,
函数(文件条目){
if(successCallback!=未定义){
successCallback(fileEntry);
}
},
函数(错误){
如果(failureCallback!=未定义){
故障回调(错误);
}
});
}
};
xhr.send();
}
函数createDirectoryRecursive(path、parentDir、callback、errorCallback){
if(path==null | | path.length==0){
回调(parentDir);
返回;
}
var index=path.indexOf('/');
var dirName=null;
var余数=null;
如果(索引=0){
var dirName=fileName.substring(0,fileName.lastIndexOf('/');
CreateDirectoryRecursive(目录名、父目录、,
功能(目录){
var name=fileName.substring(fileName.lastIndexOf('/')+1);
getFile(名称、目录、回调、errorCallback);
},错误回调);
返回;
}
parentDir.getFile(文件名,{create:true},
函数(文件条目){
log('获取的文件');
回调(fileEntry);
},
函数(错误){
if(error.code==1){
parentDir.getFile(文件名,{create:false},
函数(文件条目){
log('获取的文件');
回调(fileEntry);
},
函数(错误){
如果(错误代码==12){
返回;
}
errorCallback(错误);
});
返回;
}
errorCallback(错误);
});
}
函数readPersistantFile(文件路径、successCallback、errorCallback){
getFile(文件路径,null,
函数(文件条目){
log('获取的文件');
fileEntry.file(函数(文件){
log('文件大小:'+文件大小);
var reader=new FileReader();
reader.onloadend=函数(){
log(“成功读取文件”);
if(successCallback!=未定义){
successCallback(this.result);
}
};
var extension=filePath.substring(filePath.lastIndexOf('.')+1);
if(扩展名=='json'){
reader.readAsText(文件);
}否则{
reader.readAsDataURL(文件);
}
});
},
函数(错误){
log('获取文件时发生错误');
控制台错误(error);
if(errorCallback!=未定义){
errorCallback(错误);
}
});
}
函数writePersistantFile(文件路径、数据、isBinary、contentType、successCallback、errorCallback){
变量dataSize=(data.length!=未定义)?data.length:(data.size!=未定义)?data.size:5*1024*1024;
requestFileSystem(LocalFileSystem.PERSISTENT、dataSize、function(fs){
log('打开的文件系统:'+fs.name);
var lastIndex=filePath.lastIndexOf('/');
var directory=filePath.substring(0,lastIndex);
CreateDirectoryRecursive(目录、fs.root、函数(parentDir){
log('Directory created');
var fileName=filePath.substring(lastIndex+1);
getFile(文件名,parentDir,
函数(文件条目){
log('获取的文件');
createWriter(函数(fileWriter){
log('created writer');
如果(数据类型)=“字符串”){
write(新Blob([data],{type:contentType}));
}否则{
fileWriter.write(数据);
}
if(successCallback!=未定义){
successCallback(fileEntry);
}
});
},
函数(错误){
log('获取文件时发生错误');
控制台错误(error);
if(errorCallback!=未定义){
errorCallback(错误);
}
});
},函数(错误){
console.log('保存文件时出错');
控制台错误(error);
if(errorCallback!=未定义){
errorCallback(错误);
}
});
});
}

似乎不是上面的代码可以轻松优化,而是传递的文件夹的长度/数量

正在写入的同一文件的长度为36个字符和4个子文件夹。将其更改为单一文件名(如“page-1.jpg”)没有问题