Universal Windows Platform provides the common application platform and lets developers build apps using JavaScript/HTML, C#/XAML and C++, etc. As we all know the WinJS library helps us build Windows 10 universal Apps in HTML5 and JavaScript. The WinJS.xhr in WinJS helps wrap cross-domain calls and provides an easy mechanism to work with external web contents using HTTP operations like POST/ GET etc.
Let’s see the steps.
Create new windows 10 universal appp using WinJS. For creating a new Windows 10 universal project, refer to the following:
Now go to your default.html page and write the following code inside the body tag.
- <table style="width: 100%;border:double">
- <tr>
- <td> Enter Url Here
- <td>
- </tr>
- <tr>
- <td> <input id="txturl" type="text" style="width:800px" /> </td>
- <td> <input id="btngetfile" type="button" value="Download" /> </td>
- </tr>
- <tr>
- <td> </td>
- <td> </td>
- <td> </td>
- </tr>
- <tr>
- <td align="center">
- <div id="dvcontainer"> </div>
- </td>
- </tr>
- </table>
Here, I create one textbox to get the URL from the user, one button to perform the download operation and one div to show the downloaded image.
Now go to your
default.js file present under js folder in the solution files and write the following code inside the function tag to perform the download button click event.
- var page = WinJS.UI.Pages.define("default.html",
- {
- ready:function(element,options)
- {
- document.getElementById("btngetfile").addEventListener("click", downloadFile, false);
- },
- });
- function downloadFile() {
- var url = document.getElementById('txturl').value;
- WinJS.xhr({ url: url, responseType: "blob" })
- .done(function (r) {
- var fileURL = URL.createObjectURL(r.response);
- var image = dvcontainer.appendChild(document.createElement("img"));
- image.src = fileURL;
-
- });
- }
Here I pass the image URL and define the response type as blob to download the image in WinJS xhr method. Once the file is downloaded append the image inside the div which is already we created dvcontainer.
Now run the app and enter your image URL into the textbox and click download button it will show the output like the following screen.
For more information on Windows 10 UWP, refer to my e-book:
Read more articles on Universal Windows Platform: