There are times when you need to download content from web. Well, don’t think otherwise, I am talking about good content. In web browser this is simple, right click and save target as, isn’t it? But what if you need to right code to do it? You often need this in windows store application for downloading HTML of a webpage, contents from various api etc.. And yes, in C# you could have used web client to fulfill your need. But in case of WinJS, we have some WinJS functionality provided by WinJS library to do the same.
Let’s begin with setting our project. I have discussed this already in one of my previous post Windows 8 - The very beginning, you can have a look, in case of any problem. Once the project is set up, we are ready to code, we need to create the following function.
1: function getContentFromWeb() {
2: var link = "http://www.neelesh-vishwakarma.com";
3: WinJS.xhr({ url: link }).then(
4: function (responce) {
5: var allHtml = document.getElementById("allHtml");
6: allHtml.innerText = responce.responseText;
7: }
8: );
9: }
We are here extracting HTML of my blog. Insert the function any where in default.js file. Not just that, we need to call the function too when the app loads. So here is my whole code of default.js file.
1: (function () {
2: "use strict";
3:
4: WinJS.Binding.optimizeBindingReferences = true;
5:
6: var app = WinJS.Application;
7: var activation = Windows.ApplicationModel.Activation;
8:
9: app.onactivated = function (args) {
10: if (args.detail.kind === activation.ActivationKind.launch) {
11: if (args.detail.previousExecutionState !==
12: activation.ApplicationExecutionState.terminated) {
13: // TODO: This application has been newly launched. Initialize
14: // your application here.
15: getContentFromWeb();
16: } else {
17: // TODO: This application has been reactivated from suspension.
18: // Restore application state here.
19: }
20: args.setPromise(WinJS.UI.processAll());
21: }
22: };
23:
24: app.oncheckpoint = function (args) {
25: // TODO: This application is about to be suspended. Save any state
26: // that needs to persist across suspensions here. You might use the
27: // WinJS.Application.sessionState object, which is automatically
28: // saved and restored across suspension. If you need to complete an
29: // asynchronous operation before your application is suspended, call
30: // args.setPromise().
31: };
32:
33: app.start();
34:
35: function getContentFromWeb() {
36: var link= "http://www.neelesh-vishwakarma.com";
37: WinJS.xhr({ url: link}).then(
38: function (responce) {
39: var allHtml = document.getElementById("allHtml");
40: allHtml.innerText = responce.responseText;
41: }
42: );
43: }
44:
45: })();
Now what the function does is, it extract the content of the specified url and download it. Once the download completes, the function of the “then” part is called. Their, we put all the text we got from response to the paragraph tag we defined in the default.html page.
1: <body>
2: <p id="allHtml"></p>
3: </body>
This is simple, a quick post describing downloading content form Web.
Happy Reading!!!
0 comments