简体   繁体   English

通过XMLHttpRequest读取响应流

[英]Read response stream via XMLHttpRequest

I have the code that should read response stream and fill progress bar. 我有应该读取响应流并填充进度栏的代码。 Server response is correct. 服务器响应正确。 The issue is updateProgress event is not fired. 问题是未触发updateProgress事件。

function load_binary_resource(url) {
    var req = new XMLHttpRequest();
    req.open('GET', url, false);
    req.addEventListener("progress", updateProgress);
    req.overrideMimeType('text\/plain; charset=x-user-defined');
    req.send(null);
    if (req.status != 200) return '';
    return req.responseText;
}
function updateProgress (oEvent) {
    if (oEvent.lengthComputable) {
        console.log('loading');
        var percentComplete = oEvent.loaded / oEvent.total * 100;
        // ...
    } else {
        console.log('something happening');
    }
}

Try changing your, 尝试改变你的,

req.addEventListener("progress", updateProgress);

as, 如,

req.upload.addEventListener('progress', updateProgress, false);

Progress events exist for both download and upload transfers. 下载和上传传输均存在进度事件。 The download events are fired on the XMLHttpRequest object itself. 下载事件在XMLHttpRequest对象本身上触发。 The upload events are fired on the XMLHttpRequest.upload object as per MDN Documentation 根据MDN文档在XMLHttpRequest.upload对象上触发上载事件

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM