WinJS创建多个文件夹

时间:2014-06-24 12:32:55

标签: windows-8 windows-store-apps winjs

好的,所以我尝试创建一个深层文件夹结构,然后循环遍历重复的文件夹名称并创建这样的承诺:

var promises = [];
_.each(files, function (file) {
    promises.push(localFolder
        .createFolderAsync(folder1Name, Windows.Storage.NameCollisionOption.OpenIfExists)
        .then(function (folder1) {
            return folder1.createFolderAsync(folder2Name, Windows.Storage.CreationCollisionOption.OpenIfExists);
        })
        .then(function (folder2) {
            return folder2.createFileAsync(fileName, Windows.Storage.NameCollisionOption.replaceExisting)
        })
    );
});

return WinJS.Promise.join(promises);

问题是重复的文件夹,我认为OpenIfExists只会返回现有文件夹,但我最终会得到名为" folder1Name(1)"," folder1Name(2)&的文件夹#34;等使用FailIfExists也没有失败,所以我怀疑我的诺言链存在问题。

任何人都可以查明我做错了吗?

1 个答案:

答案 0 :(得分:1)

第一个createFolderAsync调用的第二个参数应该是Windows.Storage。 CreationCollisionOption .openIfExists而不是NameCollisionOption。

var promises = [];
_.each(files, function (file) {
    promises.push(localFolder
        .createFolderAsync(folder1Name, Windows.Storage.CreationCollisionOption.openIfExists)
        .then(function (folder1) {
            return folder1.createFolderAsync(folder2Name, Windows.Storage.CreationCollisionOption.openIfExists);
        })
        .then(function (folder2) {
            return folder2.createFileAsync(fileName, Windows.Storage.NameCollisionOption.replaceExisting)
        })
    );
});

return WinJS.Promise.join(promises);