如何使用 WinJS 一次保存多个文件?

How Can You Save Multiple Files At Once with WinJS?

我正在为 Windows Store for Windows 8 和 10 制作一个 WinJS 应用程序。我正在为我的公司制作一个应用程序,并且在用户进程结束时他们被期望单击“导出”按钮,将大约 10 个文件保存到他们选择的文件夹中。

现在,我知道如何使用 Windows.Storage.Pickers.FileSavePicker() 来保存单个文件,但我不想让用户承担接受保存 10 个文件中每一个的负担 -- 我只想用户接受他们要进入的文件夹,然后它会一口气保存它们。

这在 WinJS 中可行吗?

but I don't want to give the user the burden of accepting the save of each of the 10 files -- I just want the user to accept the folder they're going into, and then it saves them all in one swoop.

您可以使用FolderPicker to get the target folder and use StorageFolder.CreateFileAsync生成10个文件,将您想要的内容复制到文件中:

var pickers = Windows.Storage.Pickers;
var DataWriter = Windows.Storage.Streams.DataWriter;
...
document.getElementById("btnSave").onclick = function (evt)
{
       var content = "Test Content";
       var picker = new pickers.FolderPicker();
       picker.suggestedStartLocation = pickers.PickerLocationId.computerFolder;
       picker.fileTypeFilter.append(".txt");
       picker.pickSingleFolderAsync().then(function (folder) {
       if (folder != null)
       {
           for (var i = 1; i < 11; i++) {
               folder.createFileAsync("testFile" +i+ ".txt", Windows.Storage.CreationCollisionOption.replaceExisting)
                    .then(function (file) {
                        file.openAsync(Windows.Storage.FileAccessMode.readWrite)
                            .then(function (randomAccessStream) {
                                var dataWriter = new DataWriter(randomAccessStream.getOutputStreamAt(0));
                                dataWriter.writeString(content);
                                        dataWriter.storeAsync().done(function () {
                                            randomAccessStream.close();
                                            dataWriter.close();
                                });
                            }, errorHandler);
                    }, errorHandler);
                }
            }
        }, errorHandler)
}

function errorHandler(error){}