jQuery AJAX upload progress for large text fields

假装没事ソ 提交于 2019-12-04 09:04:51

You could achieve this with the new XMLHttpRequest object in HTML5 capable browsers. It supports the progress event that you could subscribe to and be notified of the AJAX operation.

Here's an example:

document.getElementById('myForm').onsubmit = function() {
    var xhr = new XMLHttpRequest();

    xhr.upload.addEventListener('progress', function(e) {
        if (e.lengthComputable) {
            var percentage = Math.round((e.loaded * 100) / e.total);
            document.getElementById('progress').innerHTML = percentage + '%';
        }
    }, false);

    xhr.open(this.method, this.action, true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            alert(xhr.responseText);
        }
    };

    xhr.send(new FormData(this));
    return false;    
};

and here's a live demo.

Maybe you can use this jquery plugin:

https://www.articlage.com/adrianillo/article/DataUploader

Just add a progressbar to upload process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!