Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

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

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 multitude of ways. In case the file becomes large, I'd like for the page to require a progress bar.

I have found numerous tutorials on creating progress bars for uploads, for PHP files, and styling them, but I haven't found anything for monitoring the percentage of an XML file that has been read and processed. I've been trying to make my code work in conjunction with this tutorial, but to no luck.

My simple AJAX call:

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

parseXML is my function to read the XML file, and then properly display it on the HTML.

Essentially I'd like to have the code automatically show the progress bar, and on success make the progress bar disappear and show the content.

If I'm approaching this the wrong way, I'm also open for any other suggestions. Sorry again if this is a simple question, but thanks for any responses.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...