Downloading different types of content (Windows Store apps using JavaScript and HTML)

You can download different types of content by when you use the responseType option of WinJS.xhr. This feature is new in XMLHttpRequest for Internet Explorer 10 and Windows Store apps built for Windows using JavaScript.

The following types are supported:

  • arraybuffer: The type of response is an ArrayBuffer. This type is used to represent binary content as an array of type Int8 or Int64, or of another integer or float type. (See Typed Arrays for more information about the different typed arrays currently supported in JavaScript.) responseText and responseXML are undefined.

    This code shows how to handle an arraybuffer response:

    WinJS.xhr({ url: "https://www.microsoft.com", responseType: "arraybuffer" })
        .done(function complete(result) {
            var arrayResponse = result.response;
            var dataview = new DataView(arrayResponse);
            var ints = new Uint32Array(arrayResponse.byteLength / 4);
    
            xhrDiv.style.backgroundColor = "#00FF00";
            xhrDiv.innerText = "Array is " + ints.length + "uints long";
        }); 
    
  • blob: The type of response is a Blob. This is used to represent binary content as a single binary entity. responseText and responseXML are undefined.

    This code shows how to handle a blob response:

    WinJS.xhr({ url: "https://www.microsoft.com/windows/Framework/images/win_logo.png", responseType: "blob" })
        .done(
            function (request) {
                 var imageBlob = URL.createObjectURL(request.response);
                var imageTag = xhrDiv.appendChild(document.createElement("image"));
          imageTag.src = imageBlob;
         });
    
  • document: The type of response is an XML Document Object Model (XML DOM) object. This is used to represent XML content, that is, content that has a MIME type of "text/xml". If the MIME type is anything other than "text/xml", the responseXML is of the same type, and responseText is undefined.

  • json: The type of response is String. This is used to represent JSON strings. responseText is also of type String, and responseXML is undefined.

  • ms-stream: The type of response is msStream, and responseText and responseXML are undefined. This response type is not defined in the W3C specification, but it is supported to make it easier to handle streaming data. For more information, see XMLHttpRequest enhancements.

  • text (the default): The type of response and responseText is String.

    This example shows how to handle a text response:

    WinJS.xhr({ url: "http://www.msdn.microsoft.com/library", responseType: "text" 
        .done(
            function (request) {
                var text = request.responseText;
                var subText = text.substring(text.indexOf("Welcome"), text.indexOf("services.") + 9);
                xhrDiv.innerHTML = subText;
        });
    

XmlHttpRequest Level 2

XMLHttpRequest enhancements

 

 

Build date: 9/4/2012