Obsługa plików z poziomu JS. Interesujące informacje nt. certyfikatów.
Files
Adding file type associations
In order to work with files, you must first declare associations for the types of files that your app works with.
package.appxmanifest –> Declarations –> File Type Associations (Available Declarations + Add), FileType (extension) (Supported File Types)
If you want to be able to programmatically access resources outside of your app package, you need to add the corresponding capability to your app manifest.
The following capabilities grant your app programmatic, read/write access to files:
- Document Library Access
- Home/work Networking
- Music Library Access
- Picture Library Access
- Removable Storage
- Video Library Access
Creating file
var sampleFile;
Windows.Storage.KnownFolders.documentsLibrary.createFileAsync("sample.txt",
Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) {
sampleFile = file;
});
Writing to file
var inputStr = id("scenario2Textarea").value;
if (sampleFile != null && inputStr != null) {
// Open file as stream with read/write access
sampleFile.openAsync(Windows.Storage.FileAccessMode.readWrite).then(function (stream) {
// File opened
// Prepare and write to the file stream
var outputStream = stream.getOutputStreamAt(stream.size);
var writer = new Windows.Storage.Streams.DataWriter(outputStream);
writer.writeString(inputStr);
// Save stream to file
writer.storeAsync().then(function() {
outputStream.flushAsync().then(function() {
// File saved! Perform additional tasks
});
});
});
}
Reading file
if (sampleFile != null) {
// Open file for reading
sampleFile.openAsync(Windows.Storage.FileAccessMode.read).then(function (stream) {
// File opened!
// Prepare to read the file
var inputStream = stream.getInputStreamAt(0);
var reader = new Windows.Storage.Streams.DataReader(inputStream);
var size = stream.size;
// File not empty
if(size > 0){
// Load and file content
reader.loadAsync(size).then(function () {
// Content loaded!
// Read content
var fileContents = reader.readString(size);
});
} else {
// Tell user file is empty
}
});
}
List of files or folders
var picturesLibrary = Windows.Storage.KnownFolders.picturesLibrary;
picturesLibrary.getItemsAsync().then(function (items) {
If you don't want all of the items, you can call getItemsAsync(startIndex, maxItemsToRetrieve) to get folder contents in a range by index.
When you call a getItemsAsync method on a folder, the list of items to receive is limited to the files and sub-folders in that folder and does not include any files and folders contained in those sub-folders.
If you want a list of the files in a folder, you can call a getFilesAsync method. If you want a list of folders, you can call a getFoldersAsync method.
outputHeader(picturesLibrary.name, items.size);
items.forEach(function (item) {
if (item.isOfType(Windows.Storage.StorageItemTypes.folder)) {
output(id(picturesLibrary.name), item.name + "\\");
}
else {
output(id(picturesLibrary.name), item.fileName);
}
});Sorted lists of files
var query = picturesLibrary.createFolderQuery(Windows.Storage.Search.CommonFolderQuery.groupByMonth);
You can call createFolderQuery(query) on your folder to create a folder query (a StorageFolderQueryResult object) to organize the files in that folder and sub-folders into virtual folders based on the CommonFolderQuery value that you specify. You can also create folder queries by calling createFolderQuery() or createFolderQueryWithOptions.
query.getFoldersAsync().then(function (monthList) {
monthList.forEach(function (month) {Files of virtual folder
var tempMonthName = month.name;
month.getFilesAsync().then(function (files) {
File Picker
The file picker is a dialog that lets you access a user's files and folders located on the local PC, connected storage devices, or HomeGroup; and also access items from apps that participate in the File Picker contract.
The file picker is specifically designed to bring user content into and out of Metro style apps and is not used for viewing and/or modifying files and folders. When an app participates in the File Picker contract, the user can select that app from the list of locations in the file picker. Your application can customize how the file picker displays content by changing the view mode. You can also configure the file picker to select folders instead of files and to save files. You can use the file picker to select a folder. You can also use the file picker to save a folder.
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
// Thumbnail view mode is best for visual files like images
openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
// Use an appropriate location from the PickerLocationId enum as the starting location
openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
// Display only specific, relevant types of files in the file picker
openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);Display the file picker by calling pickSingleFileAsync.
openPicker.pickSingleFileAsync().then(function (file) {
// Ensure picked file is valid and usable
if (file) {
// App has read/write access to file
// Your code here
} else {
// File not valid
// Your code here
}
});
Let users access your app's files through the file picker. Users will be able to select your app from the list of locations in the file picker. When selected, your app's content becomes accessible to users through a view of the file picker that is specific to your app and that you control.
Project > Add New Item... > Picker Contract
When you add the File Picker contract, Visual Studio automatically updates your "package.appmanifest" manifest file and adds the following files to your project:
- "itemPicker.js" contains the JavaScript code for your file picker view.
- "itemPicker.html" is the landing page for your file picker view.
- "itemPicker.css" contains the styles used to render the landing page for your file picker view.
<Extension Category="windows.filePicker" StartPage="filePicker.html">
<FilePicker>
<SupportedFileTypes SupportsAnyFileType="true" />
</FilePicker>
</Extension>Customization
Declarations –> Supported Declarations: FilePicker –> Supported File Types
In the Supported File Types box, uncheck the SupportsAnyFileType check box and add specific types of files that will be accessible to users through your app's file picker view.
Detecting when your app is selected in the file picker
When users select your app from the list of locations in the file picker, the system fires an activated event to load your app using its file picker view. Tip JavaScript code to handle this activated event is automatically included in the "itemPicker.js" file.
Adding and removing files from your file picker basket
Experience
Picking files. After the user picks a file, your app has persistent read/write access to that item. If desired, you can use API in Windows.Storage.AccessCache to create a Most Recently Used (MRU) list of the items chosen in the file picker.
Picking folders. Use the file picker to let the user pick a folder located on the local PC, connected storage device, or HomeGroup. After the user picks a folder, your app has persistent read/write access to that folder (and all subfolders).
Saving files. Use the file picker to have the user specify a file name, file type/extension, and folder to save a file in. After the user specifies the file characteristics in the file picker, Windows create a blank file with the user-specified file name and file name extension in the specified folder. Then your app can write the necessary data into that file.
You should not use the file picker for:
- Exploring, consuming, and managing content
- Saving when a unique file name or location is not required. Do not use the file picker to save a file if the user does not have to specify a file name or file type/extension, or when your application already knows the location to save to.
Setting certificate store capabilities
A Metro style app running in an app container has write access to only its own certificate storage. A Metro style app also has read access to local machine certificate stores other than the MY and REQUEST store.
When a smart card is inserted into a reader, the certificates and keys contained on the card are propagated to the user MY store where they can be shared by any full-trust application the user is running. By default, however, app containers do not have access to the per user MY store.
To address this issue and enable groups of principals to access groups of resources, the app container isolation model supports the capabilities concept. A capability allows an app container process to access a specific resource. The sharedUserCertificates capability grants an app container read access to the certificates and keys contained in the user MY store and the Smart Card Trusted Roots store. The capability does not grant read access to the user REQUEST store.
Certificate extension
The Certificates extension enables you to install certificates with the application, specify whether to inherit from system trust, and set certificate selection criteria.
<Extensions>
<!--Certificates Extension-->
<Extension Category="windows.certificates">
<Certificates>
<Certificate StoreName="Root" Content="myroot.cer" />
<Certificate StoreName="CA" Content="mystandca.cer"/>
<TrustFlags ExclusiveTrust="true" />
<SelectionCriteria AutoSelect="true" />
</Certificates>
</Extension>
</Extensions>
Installing
Windows.Security.Cryptography.Certificates.CertificateEnrollmentManager.installCertificate(certificate);
Installing from manifest
You can write a Metro style app that uses its own trust anchor rather than inheriting from system trust. Use the certificates extension in the manifest to identify the trust certificates you want to install when your application is deployed. The following example excludes inheritance from system trust and adds a root certificate and a standalone CA certificate to app container stores. The root need not be trusted through the Microsoft root program. The root is trusted only for your application and does not affect trust for other applications on the system.
c.d.n
Brak komentarzy:
Prześlij komentarz