Tested:
Firefox 33+ OK
Chrome 38+ OK
IE 6--ie ten Failed
Thanks to Ruan Yi Feng ' s blog:http://www.ruanyifeng.com/blog/2012/09/xmlhttprequest_level_2.html
The sample shows how to get BLOB data using a JavaScript XMLHttpRequest by sync.
The website "Tell us cannot" set Responsetype when Async is false.
from:http://www.w3.org/tr/2012/wd-xmlhttprequest-20121206/
If Async is False, the JAVASCRIPT Global environment are a document environment, and either the anonymous flag is set, the Timeout attribute value is no zero, the Withcredentials attribute value is true, or the Responsetype attribute value is n ot the empty string, throw an "Invalidaccesserror" exception and terminate these steps.
The result is a, the type of xmlhttprequest.response always string, and you must the change string to BLOB.
If you don't set "charset=x-user-defined", the string is ASCII by default. The xmlhttprequest.response isn't correct, some bytes is changed.
I set to Utf-8 or utf-16 also for test, but failed.
When using Utf-8, the length of xmlhttprequest.response was not correct
When using a utf-16, the length equals to REAL-LENGTH/2. The problem was when Real-length was odd I cannot get the last byte.
<! DOCTYPE html>
<body>
<div id= "Aabb" ></div>
<script>
var url = URL | | Window
function SaveAs (blob, filename) {
var type = Blob.type;
var force_saveable_type = ' Application/octet-stream ';
if (type && type! = Force_saveable_type) {
var slice = Blob.slice | | Blob.webkitslice | | Blob.mozslice;
Blob = Slice.call (blob, 0, Blob.size, force_saveable_type);
}
var url = url.createobjecturl (BLOB);
Console.log (URL);
var save_link = document.createelementns (' http://www.w3.org/1999/xhtml ', ' a ');
Save_link.href = URL;
Save_link.download = filename;
Save_link.text = ' FFFF ';
/*
In Firefox 33+
var m = document.getElementById (' Aabb ');
m.innerhtml = ' <a href= ' ' +url+ ' >FFF</a> ';
*/
In Chrome 38+
var event = document.createevent (' mouseevents ');
Event.initmouseevent (' Click ', True, false, window, 0, 0, 0, 0, 0, False, False, False, FALSE, 0, NULL);
Save_link.dispatchevent (event);
Url.revokeobjecturl (URL);
}
function Binarytoblob (data) {
var L = data.length, arr = new Uint8array(L);
for (var i = 0; i < L; i++) {
Arr[i] = data.charcodeat (i);
}
return new Blob ([arr], {type: ' Image/octet-stream '})
};
(function () {
var r = new XMLHttpRequest ();
R.open ("GET", ' a.tif ', false);
r.overridemimetype (' Text/plain; charset=x-user-defined '); //This was very important, the charset must be x-user-defined
R.send ();
var blob = Binarytoblob (r.response);
Blob.filetype = "Image/octet-stream";
SaveAs (Blob, ' res.tif ');
})();
</script></body>
How to get the BLOB data using JavaScript XMLHttpRequest by sync