HTML and CSS Reference
In-Depth Information
Choosing a file to load data
Once you have some files saved to disk, at some point you need to read them back. Before you can
read their content, however, you need to pick them up from storage. For this purpose, you need a
File open picker component. As mentioned, any application can serve as a File open picker as long as
it implements the File open picker contract. Most of the time, however, you will be dealing with the
system default File open picker rather than offering your own picker to other applications. Here's how
you deal with the default File open picker.
Dealing with the File open picker
The File open picker is an instance of the FileOpenPicker component. Much like the companion
FileSavePicker component, it also offers a suggestedStartLocation property for you to suggest a
preferred location where the user should start searching for a file. The following code shows how you
set up a file picker to select an image file:
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.
picturesLibrary;
openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);
The replaceAll function indicates the files you are interested in viewing in the list to select. The list
of accepted file extensions is passed in as an array. You can also set a view mode and specify that you
want file items to be represented with thumbnails.
openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
Finally, you use the pickSingleFileAsync function to display the picker's user interface and show the
user the name of the selected file:
openPicker.pickSingleFileAsync().then(function (file) {
if (file) {
TodoList.alert("You picked: " + file.name);
}
});
Figure 9-7 shows the user interface of the File open picker component. It displays the content
of the Pictures library on the current computer. As the user clicks the Pictures link to see more
locations to search for files, a drop-down menu unfolds. The menu contains the default locations
of a Windows 8 machine plus all registered custom file pickers.
By default, you find a custom picker for Photos, one for taking pictures right from the webcam.
Finally, you find the SkyDrive component that allows you to pick up a file from the cloud. All of these
are ad hoc file pickers that implement the aforementioned File open picker contract.
Search WWH ::




Custom Search