本文介绍了如何使用phonegap文件api在该目录中创建目录和文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用phonegap文件api来创建目录并在创建的目录中创建一个文件。

I am using phonegap file api to create a directory and create a file in the directory created. The directory is getting created, but the file is not getting created in the directory.

我使用的代码是:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
    var dataDir = fileSystem.root.getDirectory("data", {create: true});
    var file = dataDir.getFile("lockfile.txt", {create: true, exclusive: true});
}

目录数据已创建,但 lockfile.txt 不会被创建。

The directory data is created but lockfile.txt is not getting created.

推荐答案

您需要以异步方式调用代码:

You need to call the code in an async manner:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
   fileSystem.root.getDirectory("data", {create: true}, gotDir);
}

function gotDir(dirEntry) {
    dirEntry.getFile("lockfile.txt", {create: true, exclusive: true}, gotFile);
}

function gotFile(fileEntry) {
    // Do something with fileEntry here
}

这篇关于如何使用phonegap文件api在该目录中创建目录和文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 16:41