user1519665
user1519665

Reputation: 511

Windows Runtime Javascript Application - Launch Javascript File

In a Windows Runtime Application, I have a javascript File object created from a Javascript Blob object and it's respective mime type.

The end goal is to open this file using the default windows application (or prompt the user to choose one). So PDF's would be opened by Adobe, ect.

At the moment, I am:

  1. Creating the File object from the blob, a date object, and the mime
  2. Getting the Install Directory for the Windows Application and Creating a new File there
  3. Opening that windows file and writing my javascript-created-blob-file to it.
  4. Opening that finished windows file with Windows.System.Launcher.launchFileAsync() and having it launch the file with the proper application.

Is there any way to eliminate any of these steps? It seems redundant to save a file and open it back up when it feels like I could just "open" the file in the first place, but I don't see any documentation from Microsoft about how to just open a javascript file.

Upvotes: 0

Views: 76

Answers (1)

user1519665
user1519665

Reputation: 511

For those in the future that stumble across this, there appears to be no way to skip the write step, so here is the code to save and open a blob file given a binary blob (blob) and proper file name (fileName)

var reader = new FileReader();
var blobAsArrayBuffer;
reader.onloadend = function () {
    blobAsArrayBuffer = reader.result;
    var folder = Windows.Storage.ApplicationData.current.temporaryFolder;
    var storageFile = folder.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.replaceExisting).then(
        function (file) {
            var u8array = Uint8Array(blobAsArrayBuffer);
            Windows.Storage.FileIO.writeBytesAsync(file, u8array);
            Windows.System.Launcher.launchFileAsync(file);
        });
    };
reader.readAsArrayBuffer(blob);

Upvotes: 1

Related Questions