导航:[首页]->[wingui]->[Windows 8文件操作]

文件选择

FileOpenPicker

选择文件

// Create the picker object and set options
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
// Users expect to have a filtered view of their folders depending on the scenario.
// For example, when choosing a documents folder, restrict the filetypes to documents for your application.
openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);

// Open the picker for the user to pick a file
openPicker.pickSingleFileAsync().then(function (file) {
    if (file) {
        // Application now has read/write access to the picked file
        WinJS.log && WinJS.log("Picked photo: " + file.name, "sample", "status");
    } else {
        // The picker was dismissed with no selected file
        WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
    }
});

FileOpenPicker支持以下配置

属性访问类型描述

CommitButtonText

读取/写入Gets or sets the label text of the file open picker's commit button.

FileTypeFilter

只读Gets the collection of file types that the file open picker displays.

SettingsIdentifier

读取/写入Gets or sets the settings identifier associated with the state of the file open picker.

SuggestedStartLocation

读取/写入Gets or sets the initial location where the file open picker looks for files to present to the user.

ViewMode

读取/写入Gets or sets the view mode that the file open picker uses to display items.

ViewMode

文件的显示方式:

  1. Windows.Storage.Pickers.PickerViewMode.thumbnail : 缩略图象集。
  2. Windows.Storage.Pickers.PickerViewMode.list : 项的列表。

FileTypeFilter

文件过滤:

openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);

CommitButtonText

文件选择器提交按钮上的文本

fileOpenPicker.commitButtonText = commitButtonText;

SettingsIdentifier

如果您的应用程序使用文件打开选择器的多个实例,您可以用此属性来识别单独实例。

fileOpenPicker.settingsIdentifier = settingsIdentifier;

SuggestedStartLocation

初始目录:

成员描述
DocumentsLibrary | documentsLibrary0

文档库。

ComputerFolder | computerFolder1

计算机文件夹。

Desktop | desktop2

Windows 桌面。

Downloads | downloads3

“下载” 文件夹。

HomeGroup | homeGroup4

家庭组。

MusicLibrary | musicLibrary5

音乐库。

PicturesLibrary | picturesLibrary6

图片库。

VideosLibrary | videosLibrary7

视频库。

pickSingleFileAsync

打开单个文件,回调的参数是StorageFile类

// Open the picker for the user to pick a file
openPicker.pickSingleFileAsync().then(function (file) {
    if (file) {
        // Application now has read/write access to the picked file
        WinJS.log && WinJS.log("Picked photo: " + file.name, "sample", "status");
    } else {
        // The picker was dismissed with no selected file
        WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
    }
});

pickMultipleFilesAsync

打开多个文件,回调的参数是个数组,同pickSingleFileAsync

// Open the picker for the user to pick a file
openPicker.pickMultipleFilesAsync().then(function (files) {
    if (files.size > 0) {
        // Application now has read/write access to the picked file(s)
        var outputString = "Picked files:\n";
        for (var i = 0; i < files.size; i++) {
            outputString = outputString + files[i].name + "\n";
        }
        WinJS.log && WinJS.log(outputString, "sample", "status");
    } else {
        // The picker was dismissed with no selected file
        WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
    }
});

FolderPicker

枚举目录,只能枚举单个目录,用法同FileOpenPicker

// Create the picker object and set options
var folderPicker = new Windows.Storage.Pickers.FolderPicker;
folderPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.desktop;
// Users expect to have a filtered view of their folders depending on the scenario.
// For example, when choosing a documents folder, restrict the filetypes to documents for your application.
folderPicker.fileTypeFilter.replaceAll([".docx", ".xlsx", ".pptx"]);

folderPicker.pickSingleFolderAsync().then(function (folder) {
    if (folder) {
        // Application now has read/write access to all contents in the picked folder (including sub-folder contents)
        // Cache folder so the contents can be accessed at a later time
        Windows.Storage.AccessCache.StorageApplicationPermissions.futureAccessList.addOrReplace("PickedFolderToken", folder);
        WinJS.log && WinJS.log("Picked folder: " + folder.name, "sample", "status");
    } else {
        // The picker was dismissed with no selected file
        WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
    }
});

FileSavePicker

保存文件

属性访问类型描述

CommitButtonText

读取/写入Gets or sets the label text of the commit button in the file pickerUI.

DefaultFileExtension

读取/写入Gets or sets the default file name extension that the fileSavePicker gives to files to be saved.

FileTypeChoices

只读Gets the collection of valid file types that the user can choose to assign to a file.

SettingsIdentifier

读取/写入Gets or sets the settings identifier associated with the current FileSavePicker instance.

SuggestedFileName

读取/写入Gets or sets the file name that the file save picker suggests to the user.

SuggestedSaveFile

读取/写入Gets or sets the storageFile that the file picker suggests to the user for saving a file.

SuggestedStartLocation

读取/写入Gets or sets the location that the file save picker suggests to the user as the location to save a file.

SuggestedFileName

默认名字

// Default file name if the user does not type one in or select a file to replace
savePicker.suggestedFileName = "New Document";

FileTypeChoices

存储和过滤的文件类型

// Dropdown of file types the user can save the file as
savePicker.fileTypeChoices.insert("Plain Text", [".txt"]);

Snapped View

// Verify that we are currently not snapped, or that we can unsnap to open the picker
var currentState = Windows.UI.ViewManagement.ApplicationView.value;
if (currentState === Windows.UI.ViewManagement.ApplicationViewState.snapped &&
    !Windows.UI.ViewManagement.ApplicationView.tryUnsnap()) {
    // Fail silently if we can't unsnap
    return;
}