StreamedFileDataRequestedHandler delegate

Applies to Windows only

Represents the method that will stream data into a StorageFile when that file is first accessed.

Syntax


var streamedFileDataRequestedHandler = function(stream) {
 /* Your code */
}

Attributes

[Version(0x06020000)]

Parameters

stream

Type: StreamedFileDataRequest

The request for the streamed data in the StorageFile that was created by the CreateStreamedFileAsync or ReplaceWithStreamedFileAsync method.

Remarks

The instance of the delegate that you create will produce the data stream for the StorageFile that was returned by the CreateStreamedFileAsync or ReplaceWithStreamedFileAsync method.

When you use the CreateStreamedFileAsync or ReplaceWithStreamedFileAsync method together with an instance of StreamedFileDataRequestedHandler, you can provide the data for a file on demand instead of writing the contents of a file when you create it. That is, you can defer expensive operations to produce the data for the file until the file is first accessed.

Examples

The following simple example demonstrates how to create an instance of the StreamedFileDataRequestedHandler delegate. The example also shows how this instance produces the data stream for the StorageFile that was returned by the CreateStreamedFileAsync when the file is first accessed.

This simple example is intended only to demonstrate the programming pattern for using the StreamedFileDataRequestedHandler delegate. For a small file with data that's easy to produce, the streaming pattern is not useful.


using Windows.Storage;
using Windows.Storage.Streams;
...
        private async void CreateStreamedFile()
        {
            // Create a streamed file.
            StorageFile file =
                await StorageFile.CreateStreamedFileAsync("file.txt",
                    StreamedFileWriter, null);

            // Prepare to copy the file.
            StorageFolder localFolder = ApplicationData.Current.LocalFolder;
            string newName = "copied_file.txt";

            // Copy the streamed file. At this point,
            // the data is streamed into the source file.
            await file.CopyAsync(localFolder, newName,
                NameCollisionOption.ReplaceExisting);
        }

        private async void StreamedFileWriter(StreamedFileDataRequest request)
        {
            try
            {
                using (var stream = request.AsStreamForWrite())
                using (var streamWriter = new StreamWriter(stream))
                {
                    for (int l = 0; l < 50; l++)
                    {
                        await streamWriter.WriteLineAsync("Data.");
                    }
                }
                request.Dispose();
            }
            catch (Exception ex)
            {
                request.FailAndClose(StreamedFileFailureMode.Incomplete);
            }
        }


Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

None supported

Namespace

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

Metadata

Windows.winmd

 

 

Show:
© 2014 Microsoft