Search code examples
windows-8windows-runtimewinjs

How to share a fetched image?


I do a WinJS.xhr() call with a responseType of "blob" and the result.response is an image. How do I share that image? The DataTransferManager's setBitmap wants a streamreference. Do I need to record the image to a temporary file and then do a Windows.Storage.Streams.RandomAccessStreamReference.createFromFile()? If so, how do I write the image to a StorageFile? Thanks.


Solution

  • You have two choices:

    1. Writing the file to disk. See this question for details
    2. Create a multiple use blob from the XHR response. See MSDN for details.

    The second option stops you from having to write the file to disk if your just sharing it temporarily, but it does some with some memory management to revoke the blob later. With the URL created, you can just assign it to the an img tags src attribute.