How would you create a progress bar with AJAX/jQuery for downloading XML content?

后端 未结 2 831
-上瘾入骨i
-上瘾入骨i 2020-12-17 07:50

I apologize if this is a rather simple answer, but I\'ve been stuck on it for a while. Currently I have an XML file where users can add images, audio files, and text in a mu

相关标签:
2条回答
  • 2020-12-17 07:59

    If you could move to websockets, I think you'd get more ability to manage the data transfer in a single thread.

    If you want traditional ajax, I think you need an approach similar to the one typically used for upload progress:

    http://phpmaster.com/tracking-upload-progress-with-php-and-javascript/

    Basically, poll the server and ask it how far along it is in the transfer, then report that to the user. So you'd have your large ajax request while simultaneously polling a different small endpoint. Your server keeps the upload progress as part of the user session.

    0 讨论(0)
  • This should be enough to do it:

    $(document).ready(function() {
        showProgressBar();
        $.ajax({
            type: "GET",
            url: xmlTitle,
            dataType: "xml",
            success: parseXML
        });
        function parseXML(xml){
            hideProgressBar();
            ...
        }
    });
    

    If you wanted to actually update the progressbar with the real progress of the upload, you'll have to use the xhr.upload.progress event and the xhr.progress event, neither of which are implemented in jQuery because there is no workaround to make them work in IE<10.

    0 讨论(0)
提交回复
热议问题