You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Cordova Android中用JavaScript创建文本文件遇问题求助

Cordova File插件创建JSON备份文件失败排查

我尝试创建一个仅存储JSON数据的备份文本文件,已安装Cordova File插件,试了多段代码始终无法定位问题。以下是相关代码:

HTML代码

<body onload="init()">

<section class="head">
    <div class="head-img">
        <img src="src/img/bag.jpg">
    </div>
    <div class="searchbar">
        <input type="text" placeholder="Suchen...">
        <img src="src/img/lupe.png">
    </div>
    <div class="backup-container">
        <button onclick="createBackup()" class="btn">Backup erstellen</button>
        <label for="upload" class="btn">Backup importieren</label>
        <input type="file" class="hidden" id="upload" onclick="loadBackup()">
    </div>
</section>

JS代码

function createBackup() {

    let backup = JSON.stringify(bags);

    //get the file system object
    let fileSystem = cordova.file.dataDirectory;
    
    //create the file
    window.resolveLocalFileSystemURL(fileSystem, function(directoryEntry) {
        directoryEntry.getFile("demo.txt", {create: true}, function(fileEntry) {
    
            //write the data to the file
            fileEntry.createWriter(function(writer) {
                writer.write(backup);
            }, function(error) {
                console.log("Error creating writer: " + error);
            });
    
        }, function(error) {
            console.log("Error creating file: " + error);
        });
    }, function(error) {
        console.log("Error getting file system: " + error);
    });
    
  }

bags变量定义在另一个JS文件中,我认为这不是问题的关键。


内容的提问来源于stack exchange,提问作者Vision Flow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 11:37:28