6

I need to AJAX load big binary files, hence I would like to show a progress indicator. I've read the lengthComputable field of the Progress.event parameter is true when there'sa "Content-Length" header set. I have this header, but lengthComputable is always false. What I'm doing wrong?.

Here's the code:

function onProgress(evt){
    if(evt.lengthComputable){
        this.progress = (evt.loaded / evt.total)*100; 
        // do something  ...
    }
}

function load(url){
    var me = this;
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'arraybuffer';   
    xhr.onload = this.load;
    xhr.onprogress = this.updateProgress;
    xhr.send();
}

And this is the header (from Chrome):

Request URL:http://localhost/cgi-bin/test.cgi
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:es-419,es;q=0.8
Connection:keep-alive
Cookie:__utma=1.1581312881.1342448904.1342729430.1342812228.5
Host:localhost
Referer:http://localhost/cgi-bin/test.cgi
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.35 (KHTML, like Gecko) Chrome/27.0.1448.0 Safari/537.35
Query String Parametersview sourceview URL encoded
idimage:3431
Response Headersview source
Connection:Keep-Alive
Content-Encoding:deflate
Content-Length:11825252
Content-Type:application/binaryfile
Date:Fri, 05 Apr 2013 11:36:06 GMT
Keep-Alive:timeout=5, max=98
Server:Apache/2.2.22 (FreeBSD) mod_ssl/2.2.22 OpenSSL/0.9.8x DAV/2 PHP/5.3.10 with Suhosin-Patch mod_python/3.3.1 Python/2.7.3 mod_perl/2.0.5 Perl/v5.10.1

EDIT: It looks like the problem is the Content-Encoding, in my case deflate: How can I use deflated/gzipped content with an XHR onProgress function?

Community
  • 1
  • 1
leonardorame
  • 1,131
  • 4
  • 18
  • 35
  • I can confirm the problem was the Content-Encoding. I'll have to decompress client-side using JS. – leonardorame Apr 05 '13 at 12:39
  • If you are setting a content-length header that is being ignored, you must also set the buffer size of your response to be able to hold the bytes your are sending. Otherwise in HTTP 1.1 it will revert to chunking the response. – J. Allen Aug 11 '17 at 19:39

0 Answers0