locked
video download progress calculation in winxhr

    Question

  • hi all

    I am using following code to download video i am getting blob data successfully but i am not able to calculate progress percentage. please assist me.

                                                                                                                                                                                                          

    WinJS.xhr({ url: vurl, responseType: "blob" })
                                                .done(function complete(result) {

    //do something with blob
                                                },
                                                    function error(error) {
    //log error
                                                    },
                                                    function progress(result) {
                                                        if (result.readyState > 2) {
                                                            //var totalBytes = result.getResponseHeader('Content-length');
                                                            //var dlBytes = result.responseText.length;
                                                            //var per = (totalBytes > 0) ? (Math.round((dlBytes / totalBytes) * 100) + "%") : (Math.round(dlBytes / 1024) + "K");
                                                            //$("#vmDownloadHeading").html("Downloading Media " + per);
                                                        }

                                                    });

    if i use response type as text i can calculate progress by above code, for other response it will be undefined so how to calculate the progress?

    regards

    vinay



    • Edited by VinayHs Friday, February 21, 2014 5:00 AM
    Friday, February 21, 2014 4:57 AM

Answers

  • Try using XMLHttpRequest directly in your code. For example the below approach gives you the progress information as you'd expect. Basically you want to look at the e.loaded and the e.total properties in the progress event to get what you need.

            var xhr = new XMLHttpRequest();
            xhr.open('GET', '...path to image...', true);
            xhr.responseType = 'blob';
    
            xhr.onload = function(e) {
                if (this.status == 200) {
                    // image downloaded successfully!                
                }
            };
    
            xhr.onprogress = function (e) {
                console.log("Downloaded: " + e.loaded + " of " + e.total);
            };
    
            xhr.send();


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Friday, February 21, 2014 9:23 PM
    Moderator