在cordova / phonegap中创建和保存txt文件

时间:2019-04-12 08:55:06

标签: javascript cordova phonegap

我想在我的cordova / phonegap应用程序中创建一个存储纯文本的文本文件。我到处搜索,几乎找不到任何显示此过程的示例。我正在使用此代码,当我单击“写入文件”时,警报显示该文件已保存到“ file:///data/user/0/com.adobe.phonegap.app/files/files/hello_world.txt” '但是当我检查文件夹时,那里什么也没有。这是我正在使用的代码。

我是config.xml

<preference name="AndroidPersistentFileLocation" value="Internal" />

我的html按钮

<button class="btn-lg btn-success" onclick="saveFile('hello_world.txt', 'This is Dummy Content')">Write File</button>

脚本正在使用

function saveFile(fileName, fileData) {
    // Get access to the file system
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
        // Create the file.
        fileSystem.root.getFile(fileName, {create: true, exclusive: false}, function (entry) {
            // After you save the file, you can access it with this URL
            var myFileUrl = entry.toURL()
            entry.createWriter(function (writer) {
                writer.onwriteend = function (evt) {
                    alert("Successfully saved file to " + myFileUrl)
                }
                // Write to the file
                writer.write(fileData)
            }, function (error) {
                alert("Error: Could not create file writer, " + error.code)
            })
        }, function (error) {
            alert("Error: Could not create file, " + error.code)
        })
    }, function (evt) {
        alert("Error: Could not access file system, " + evt.target.error.code)
    })
}

0 个答案:

没有答案