How to get the BLOB data using JavaScript XMLHttpRequest by sync

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.