cordova文件系统覆盖字符串

时间:2018-08-15 08:18:14

标签: cordova phonegap filewriter

我正在使用FileWriter将两个字符串分别保存到一个文件中。当我手动删除文件时,一切都会按预期进行。但是,一旦文件存在,FileWriter不会正确覆盖。

代码:

    $('#buttonExport').click(function () {

    let exportLines = createFeatureCollection(splitLinesArray);
    let exportPoints = JSON.stringify(duplicatedLayer);

    let exportArray = [exportLines,exportPoints];
    let exportNameArray = ['routes.geojson','points.geojson'];

    for (let i =0; i< exportArray.length; i++) {
        try {
            window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 10 * 1024 * 1024, gotFS, failFS);

        } catch (e) {
            console.log('Error:', e);
        }

        function gotFS(fileSystem) {

            fileSystem.root.getFile(exportNameArray[i], {
                create: true,
                exclusive: false
            }, gotFileEntry, failFS);


        }

        function gotFileEntry(fileEntry) {
            fileEntry.createWriter(gotFileWriter, failFS);
        }

        function gotFileWriter(writer) {
            //writer.seek(0);
            writer.write(exportArray[i]);

        }

        function failFS(error) {
            console.log(error.code);
        }
    }
});
第一次导出后

points.geojson(有效)

{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"infoText":"","imageName":""},"geometry":{"type":"Point","coordinates":[17.578125000000004,48.22467264956519]}}

第二次导出后的points.geojson(无效,缺少字符串):

{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"infoText":"","imageName":""},"geometry":{"type":"Point","coordinates":[17.578125000000004,48.22467264956519]}},{

我每次尝试将文件指针重置为开头

writer.seek(0);

writer.truncate(0);

两者均对行为没有任何影响。我的循环有问题吗?

0 个答案:

没有答案