Receiving a File in Windows Store Apps

Introduction

In this article I describe how to create a Windows Store App for receiving a file using JavaScript. In File Explorer, create a file and double-click the file. This application is brought to the foreground. The filename is displayed in the output field. In my previous article I described how to launch a file in Windows Store apps. For this visit, Launching a file in windows Store using Java Script.

I assume you can create a simple Windows Store App using JavaScript. For more help visit Simple Windows Store Apps using JavaScript.

To start the creation of the app, add one JavaScript page by right-clicking on the js folder in the Solution Explorer and select Add > new item > JavaScript Page and then give an appropriate name. In the same way, add one HTML page to your project.

receving-a-file-in-windows-store-apps.jpg

Write the following code in default.html:

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8" />

    <title></title>

    <link rel="stylesheet" href="//Microsoft.WinJS.1.0/css/ui-light.css" />

    <script src="//Microsoft.WinJS.1.0/js/base.js"></script>

    <script src="//Microsoft.WinJS.1.0/js/ui.js"></script>

    <link rel="stylesheet" href="/css/default.css" />

    <script src="/js/script1.js"></script>

    <script src="/js/default.js"></script>

</head>

<body role="application" style="background-color: lightpink">

    <center><div id="rootGrid">

        <div id="content">

            <h1 id="featureLabel"></h1>

            <div id="contentHost"></div>

        </div>

    </div></center>

</body>

</html>

Write the following code in default.js:
 

(function () {

    "use strict";

 

    var appTitle = "";

 

    var pages = [

        { url: "page.html" }

    ]; 

    function activated(e) {

        var url = null;

        var arg = null;

        if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.file) {

            url = pages[2].url;

            arg = e.detail.files;

        } else if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.protocol) {

            url = pages[3].url;

            arg = e.detail.uri;

        } else if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {

            url = WinJS.Application.sessionState.lastUrl || pages[0].url;

        }

        if (url !== null) {

            e.setPromise(WinJS.UI.processAll().then(function () {

                return WinJS.Navigation.navigate(url, arg);

            }));

        }

    }

    WinJS.Navigation.addEventListener("navigated", function (evt) {

        var url = evt.detail.location;

        var host = document.getElementById("contentHost");

        host.winControl && host.winControl.unload && host.winControl.unload();

        WinJS.Utilities.empty(host);

        WinJS.UI.Pages.render(url, host, evt.detail.state).done(function () {

            WinJS.Application.sessionState.lastUrl = url;

        });

    });

    WinJS.Namespace.define("App", {

        appTitle: appTitle,

        pages: pages

    });

    WinJS.Application.addEventListener("activated", activated, false);

    WinJS.Application.start();

})();


Write the following code in page.html:

<!DOCTYPE html>

<html>

<head>

    <title></title>

    <link rel="stylesheet" href="/css/receive-file.css" />

    <script src="/js/script.js"></script>

</head>

<body>

    <div data-win-control="App.pageOutput">

    </div>

</body>

</html> 


Write the following code in script.js:

(function () {

    "use strict";

    var page = WinJS.UI.Pages.define("page.html", {

        processed: function (element, files) {

            if (files) {

                WinJS.log && WinJS.log("File activation received. The number of files received is " + files.size + ". The first received file is " + files[0].name + ".", "app", "status");

            }

        },

 

        ready: function (element, options) {

        }

    });

})();

Summary

In this app I described receiving a file in a Windows Store app using JavaScript. I hope this article has helped you to understand this topic. Please share if you know more about this. Your feedback and constructive contributions are welcome.

Up Next
    Ebook Download
    View all
    Learn
    View all