[ This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation ]
Learn how to add file-handling capabilities to your Windows Store app using JavaScript and running on Windows 8.1.
Each section of this topic describes a key file-handling feature, links to a topic that provides more detail on that feature, and offers info about how to quickly find the relevant code in this topic's companion File handling, start to finish companion sample.
Note Many of the functions used to interact with folders and files are asynchronous. You can learn how to write asynchronous JavaScript apps in Asynchronous programming in JavaScript.
File access basics: enumerating, getting properties, and reading and writing data
You can access files in a location such as a folder, library, device, or network location, with just a single line of code—a call to the StorageFolder.getFilesAsync function. See Accessing files programmatically for step-by-step instructions that show how to perform tasks like enumerating the top-level files and folders of a specified location and querying files in a location.
These steps show you how to enumerate files in a particular location:
This screen shot from the companion sample shows an example of enumerating the files in Pictures.
Find it in the sample: The sample includes a page titled FileAccessBasics, which includes the examples presented in this section. The relevant JavaScript and HTML code is centralized to the FileAccessBasicsPage.js (OnEnumPicturesClick function) and FileAccessBasicsPage.html files.
File properties describe or quantify an attribute of a file or its contents. For example, file properties include data such as file name, path, file size, file attributes, and date last accessed. Quickstart: Getting a file's properties shows how to retrieve and display a file's top-level and basic properties.
These steps show you how to get either top-level or basic file properties:
This code example enumerates all files in Pictures and displays various top-level and basic file properties. Note how JavaScript promises are being used to synchronize the results of two asynchronous operations (StorageFolder.getFilesAsync and StorageFile.getBasicPropertiesAsync). To learn more about asynchronous programming and promises in JavaScript, refer to Asynchronous programming in JavaScript.
This screen shot from the companion sample shows an example of getting various top-level and basic file properties.
Find it in the sample: The sample includes a page titled FileAccessBasics, which includes the examples presented in this section. The relevant JavaScript and HTML code is centralized to the FileAccessBasicsPage.js (OnGetFilePropertiesClick function) and FileAccessBasicsPage.html files.
A Windows Store app reads and writes files via the FileIO class. See Quickstart: Reading and writing a file for code examples that show how to read and write various types of data using the FileIO and StorageFile classes.
Writing text to a file
To write to a file, you must first acquire a StorageFile object to pass to one of the FileIO functions for writing data. In the companion sample, this is done by creating a file via the StorageFolder.createFileAsync function.
Once you have a StorageFile object, you can write text to it underlying file via one of the overloaded FileIO.writeTextAsync functions.
This code example writes the current date/time to a sample file.
To read the contents of a file, you must first acquire a StorageFile object to pass to one of the FileIO functions for reading data. In the companion sample, this is done by calling the StorageFolder.getFileAsync function.
Once you have a StorageFile object, you can read text from its underlying file via one of the overloaded FileIO.readTextAsync functions.
This code example reads the contents from a sample file.
This screen shot shows an example of running the sample and writing to the sample file.
Find it in the sample: The sample includes a page titled FileAccessBasics, which includes the samples presented in this section. The relevant JavaScript and HTML code is centralized to the FileAccessBasicsPage.js (OnWriteTextToFileClick and OnReadTextFromFileClick functions) and FileAccessBasicsPage.html files.
Pickers—both file pickers and folder pickers—are used to display a list of files or folders from which users can select one or more items for further processing. Pickers can be configured programmatically to search for files and folders that match a specified filter (such as files with specific extensions), start at a particular folder, display a specific view mode (list or thumbnail), and much more.
The following procedures show you how to configure the various pickers for single-file, multiple-file, and single-folder selection.
Call the FileOpenPicker.pickSingleFileAsync function. When the FileOpenPicker.pickSingleFileAsync function is complete, the app has read/write access to the selected file.
This code example instantiates and displays a file picker for single-file selection.
Call the FileOpenPicker.pickMultipleFileAsync function. When the FileOpenPicker.pickMultipleFileAsync function is complete, the app has read/write access to the selected files. The files selected are represented by an array of StorageFile objects. The array's sizes property tells you how many files were selected, so you can use a for loop with standard array notation to access each StorageFile object.
This code example instantiates and displays a file picker for multiple-file selection.
This screen shot shows the results of running the sample and selecting two files (sample1.png and sample2.png).
Find it in the sample: The sample includes a page titled File and Folder Pickers that demonstrates the tasks outlined in this section. The JavaScript code and HTML for this sample is in the FilePickerPage.js and FilePickerPage.html files, respectively.
Windows 8.1 enables users to mark OneDrive files as online only. When the user is disconnected from OneDrive, these files are not available. To help you programmatically determine the availability of a file, there's a new property called StorageFile.isAvailable.
These steps show you how to determine the availability of files using the StorageFile.isAvailable property.
This screen shot shows sample results of checking the files in Pictures for availability.
Find it in the sample: The sample includes a page titled "OneDriveFilesPage", which has a button that enumerates all of the files in the local machine's Pictures. Each file name is displayed along with that file's provider (for example, This PC or OneDrive), and whether or not the file is currently available. The JavaScript code and HTML for this sample are in the OneDriveFilesPage.js and OneDriveFilesPage.html files, respectively.
Recommended. Running the Windows App Certification Kit helps you make sure that your app fulfills Windows Store requirements. We recommend you run it whenever you add major functionality to your app.