AJAX response gives corrupted compressed (.tgz) file

We implement a client-side web application that communicates with the server exclusively through XMLHttpRequests (and the AJAX engine).

XHR responses are usually plain text with some XML, but in this case, the server sends the compressed data to a .tgz file type. We know for sure that the data sent by the server is correct, because if we use the HTTP command line client, such as curl, the file sent as a response is valid and contains the expected data.

However, when calling AJAX and "swinging" the response in the downloaded file, the resulting file differs in size (higher) than the correct one, and it is not recognized by the decompressor. It accepts the following error:

gzip: stdin: not in gzip format
/bin/gtar: Child returned status 1
/bin/gtar: Error is not recoverable: exiting now

The code I use is as follows:

*$.AJAX*.done(function(data){
    window.URL = window.webkitURL || window.URL;
    var contentType = 'application/x-compressed-tar';
    var file = new Blob([data], {type: contentType});
    var a = document.createElement('a'),
    ev = document.createEvent("MouseEvents");
    a.download = "browser_download2.tgz";
    a.href = window.URL.createObjectURL(file);
    ev.initMouseEvent("click", true, false, self, 0, 0, 0, 0, 0,
            false, false, false, false, 0, null);
    a.dispatchEvent(ev);
});

, AJAX, , , . contentType, , curl, . , : , ( , ). , .

tgz , , (EF, BF BD, ), .

:

(a) , , .

(b) , , "Accept-Encoding: gzip, deflate"; , (Firefox ) .

(c) , , ; / .

Edit

:

(a) : http://en.webhex.net/view/278aac05820c34dfbdd2217c03970dd9/0 (b) () : http://en.webhex.net/view/4a01894b814c17d2ec71ba49ac48e683

+4
1

, -, .

Javascript Unicode/ASCII; , ( , EF, BF .. Hex Viewer, ASCII/Unicode),

. javascript-, ( ). , , XMLHttpRequest , , , . , :

var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.responseType = 'arraybuffer';

, responseType "arraybuffer". , JQuery AJAX. , , Jquery, (overrideMimeType, - , ). XMLHttRquest .

+10

All Articles