TargetFileRequest TargetFileRequest TargetFileRequest TargetFileRequest Class

Lets an app that provides a save location specify the StorageFile that represents the file to save and get a deferral so the app can respond asynchronously to a TargetFileRequested event.

Syntax

Declaration

public sealed class TargetFileRequestpublic sealed class TargetFileRequestPublic NotInheritable Class TargetFileRequest

Remarks

If your app participates in the File Save Picker contract and a TargetFileRequested event fires, your app should create a new StorageFile that represents the file the user wants to save. The name of the StorageFile you create must match the name of the file specified by the FileName property. The StorageFile you create is returned to the app that called the file picker (the calling app) so that the calling app can write content to the file. Learn more about responding to this event in TargetFileRequestedEventArgs.

Examples

The File picker sample demonstrates how to respond to a TargetFileRequested event handler, including how to use the Request property to get the TargetFileRequest.


// Event handler
private async void OnTargetFileRequested(FileSavePickerUI sender, TargetFileRequestedEventArgs e)
{
    // Respond to TargetFileRequested event on the background thread on which it was raised

    // Requesting a deferral allows the app to call another asynchronous method and complete the request at a later time
    var deferral = e.Request.GetDeferral();

    // Create file and assign to TargetFile property
    e.Request.TargetFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(sender.FileName, CreationCollisionOption.GenerateUniqueName);

    // Complete the deferral to let the Picker know the request is finished
    deferral.Complete();
}

// Register for the event
fileSavePickerUI.TargetFileRequested += new TypedEventHandler<FileSavePickerUI, TargetFileRequestedEventArgs>(OnTargetFileRequested);

// Event handler
function onTargetFileRequested(e) {
    var deferral;
    deferral = e.request.getDeferral();

    // Create a file to pass to the file picker which then gives it to the calling app
    Windows.Storage.ApplicationData.current.localFolder.createFileAsync(fileSavePickerUI.fileName).done(function (file) {
            // Assign the resulting file to the targetFile property and complete the deferral to indicate success
        e.request.targetFile = file;
        deferral.complete();
    }, function () {
        // Set the targetFile property to null and complete the deferral to indicate failure
        e.request.targetFile = null;
        deferral.complete();
    });
};

// Register for the event
fileSavePickerUI.addEventListener("targetfilerequested", onTargetFileRequested, false);

In the example, e contains a TargetFileRequestedEventArgs object.

Properties summary

Gets or sets the IStorageFile object that is provided to represent the file to save by the app that is providing the save location.

Methods summary

Gets a TargetFileRequestDeferral that the app providing the save location can use to respond asynchronously to a TargetFileRequested event.

Properties

  • TargetFile
    TargetFile
    TargetFile
    TargetFile

    Gets or sets the IStorageFile object that is provided to represent the file to save by the app that is providing the save location.

    public IStorageFile TargetFile { get; set; }public IStorageFile TargetFile { get; set; }Public ReadWrite Property TargetFile As IStorageFile

    Property Value

    Remarks

    If your app (as the provider of the save location) can't provide an object for the file to save, set this property to null. Unless the user selects an existing file to save, your app should create a StorageFile object to represent the file sets this property to that object.

    The file name and extension of the object that represents the file to save must match the file name and extension specified by the user (and accessible through the FileName property) or the attempt to save the file will fail. If the attempt fails, the user can try to save the file again.

Methods

Device family

Windows 10 (introduced v10.0.10240.0)

API contract

Windows.Foundation.UniversalApiContract (introduced v1)

Attributes

Windows.Foundation.Metadata.ContractVersionAttribute

Details

Assembly

Windows.Storage.Pickers.Provider.dll