How to get blob data using javascript XmlHttpRequest by sync

Tested:

Firefox 33+ OK

Chrome 38+ OK

IE 6 — IE 10 Failed

Thanks to 阮一峰’s blog: http://www.ruanyifeng.com/blog/2012/09/xmlhttprequest_level_2.html

The sample shows how to get blob data using javascript XmlHttpRequest by sync.

The w3c 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 is a document environment, and either the anonymous flag is set, the timeout attribute value is not zero, the withCredentials attribute value is true, or the responseType attribute value is not the empty string, throw an “InvalidAccessError” exception and terminate these steps.

The result is that the type of XMLHttpRequest.response always string, you must change string to blob.

if you not set “charset=x-user-defined”, the string is ascii by default. The XMLHttpRequest.response is not correct, some bytes are changed.

I set to utf-8 or utf-16 also for test, but failed.

when using utf-8, the length of XMLHttpRequest.response is not correct
when using utf-16, the length equals to Real-Length/2. the problem is when Real-Length is odd I cannot get the last byte.

Original: https://www.cnblogs.com/huqingyu/p/4090434.html
Author: 浙林龙哥
Title: How to get blob data using javascript XmlHttpRequest by sync

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/535812/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球