Click here to Skip to main content
15,895,807 members
Articles / Web Development / HTML5

Windows 8 JavaScript Metro Application–Getting Started

Rate me:
Please Sign up or sign in to vote.
4.98/5 (24 votes)
12 Mar 2012Ms-PL12 min read 124.7K   6.5K   74  
Getting Started with Windows 8 Metro App Development in JavaScript
/// <resource type="javascript" src="//Microsoft.WinJS.0.6/js/base.js" shared="true" />
/// <resource type="javascript" src="//Microsoft.WinJS.0.6/js/ui.js" shared="true" />

(function () {
    "use strict";
    var folder = Windows.Storage.ApplicationData.current.localFolder;

    function download(imgUrl, imgName) {
        return WinJS.xhr({ url: imgUrl, responseType: "blob" }).then(function (result) {
            var blob = result.response;
            return folder.createFileAsync(imgName, Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) {
                // Open the returned file in order to copy the data
                return file.openAsync(Windows.Storage.FileAccessMode.readWrite).then(function (stream) {
                    return Windows.Storage.Streams.RandomAccessStream.copyAsync(blob.msDetachStream(), stream).then(function () {
                        // Copy the stream from the blob to the File stream
                        return stream.flushAsync().then(function () {
                            stream.close();
                        });
                    });
                });
            });
        }, function (e) {
            var msg = new Windows.UI.Popups.MessageDialog(e.message);
            msg.showAsync();
        });
    }

    function fileExists(fileName) {
        return folder.getFileAsync(fileName).then(function (file) {
            return file;
        }, function (err) {
            return null;
        });
    }

    WinJS.Namespace.define('imgDownloader', {
        download: download,
        fileExists: fileExists
    });
})();

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Architect Sela
Israel Israel
Shai Raiten is VS ALM MVP, currently working for Sela Group as a ALM senior consultant and trainer specializes in Microsoft technologies especially Team System and .NET technology. He is currently consulting in various enterprises in Israel, planning and analysis Load and performance problems using Team System, building Team System customizations and adjusts ALM processes for enterprises. Shai is known as one of the top Team System experts in Israel. He conducts lectures and workshops for developers\QA and enterprises who want to specialize in Team System.

My Blog: http://blogs.microsoft.co.il/blogs/shair/

Comments and Discussions