Skip to main content
FileSavePickerUI.TargetFileRequested | targetfilerequested event

Applies to Windows and Windows Phone

Fires when the user commits a file to be saved in the file picker.

Syntax


function onTargetFileRequested(eventArgs) { /* Your code */ }
 
// addEventListener syntax
fileSavePickerUI.addEventListener("targetfilerequested", onTargetFileRequested);
fileSavePickerUI.removeEventListener("targetfilerequested", onTargetFileRequested);
 
- or -

fileSavePickerUI.ontargetfilerequested = onTargetFileRequested;

public event TypedEventHandler<FileSavePickerUI, TargetFileRequestedEventArgs> TargetFileRequested

Public Event TargetFileRequested As TypedEventHandler(Of FileSavePickerUI, TargetFileRequestedEventArgs)

public:
event TypedEventHandler<FileSavePickerUI, TargetFileRequestedEventArgs>^ TargetFileRequested {
   Windows::Foundation::EventRegistrationToken add(TypedEventHandler<FileSavePickerUI, TargetFileRequestedEventArgs>^ value);
   void remove(Windows::Foundation::EventRegistrationToken token);
}

Event information

Delegate TypedEventHandler<FileSavePickerUI, TargetFileRequestedEventArgs>

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.

Note  File picker UI is disabled until the providing app has finished responding to this event.

Examples

The File picker sample demonstrates how to respond to a targetfilerequested event handler.


// 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.

Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Phone Silverlight 8.1 and Windows Runtime apps]

Namespace

Windows.Storage.Pickers.Provider
Windows::Storage::Pickers::Provider [C++]

Metadata

Windows.winmd

See also

FileSavePickerUI class
TargetFileRequestedEventArgs class
Windows.Storage.StorageFile class