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.

<!DOCTYPE HTML>
<html>
<head>
</head>
<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 line is 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></html>

时间: 2024-10-27 12:36:33

How to get blob data using javascript XmlHttpRequest by sync的相关文章

javascript XMLHttpRequest对象全面剖析(转)

一. 引言 异步JavaScript与XML(AJAX)是一个专用术语,用于实现在客户端脚本与服务器之间的数据交互过程.这一技术的优点在于,它向开发者提供了一种从Web服务器检索数据而不必把用户当前正在观察的页面回馈给服务器.与现代浏览器的通过存取浏览器DOM结构的编程代码(JavaScript)动态地改变被显示内容的支持相配合,AJAX让开发者在浏览器端更新被显示的HTML内容而不必刷新页面.换句话说,AJAX可以使基于浏览器的应用程序更具交互性而且更类似传统型桌面应用程序. Google的G

caffe出错:Unknown bottom blob &#39;data&#39; (layer &#39;conv1&#39;, bottom index 0)

原文https://blog.csdn.net/u011070171/article/details/75425740 caffe训练出现如下错误: Unknown bottom blob 'data' (layer 'conv1', bottom index 0) 问题,缺少对应的data. 解决:可能是在训练的时候加入测试迭代数,可是网络文件里并没有测试网络. 检查solver.prototxt里边是否定义了test相关内容,而train.prototxt里边并没有test相关结构. caf

javascript XMLHttpRequest对象全面剖析

转载:http://www.jb51.net/article/23175.htm 一. 引言 异步JavaScript与XML(AJAX)是一个专用术语,用于实现在客户端脚本与服务器之间的数据交互过程.这一技术的优点在于,它向开发者提供了一种从Web服务器检索数据而不必把用户当前正在观察的页面回馈给服务器.与现代浏览器的通过存取浏览器DOM结构的编程代码(JavaScript)动态地改变被显示内容的支持相配合,AJAX让开发者在浏览器端更新被显示的HTML内容而不必刷新页面.换句话说,AJAX可

javascript XMLHttpRequest 对象的open() 方法参数说明

XMLHttpRequest.open() 初始化 HTTP 请求参数 语法open(method, url, async, username, password) method 参数是用于请求的 HTTP 方法.值包括 GET.POST 和 HEAD. ( 大小写不敏感. POST:用"POST"方式发送数据,可以大到4MBGET:用"GET"方式发送数据,只能256KB如果请求带有参数的化实用POST方式,POST方式将参数放置在页面的隐藏控件内没有参数使用GE

directly receive json data from javascript in mvc

if you send json data to mvc,how can you receive them and parse them more simply? you can do it like this: latestData = []; $('.save').click(function () { $('.content tr').each(function () { var item = { id:null,date: '', weekday: '', holiday: ''}; l

ASP.NET MVC - loop model data in javascript

Key: razor syntax using @: before the js variable in c# code block Example: var chartData = []; @for(int i=0; i < Model.ModuleDetails.Count; i++) { @: chartData.push(@Html.Raw(Json.Encode(Model.ModuleDetails[i].ChartData.ToArray()))); } reference: ht

JavaScript: Use a Web Proxy for Cross-Domain XMLHttpRequest Calls

The XMLHttpRequest object (also known as the XMLHTTP object in Internet Explorer) is at the core of today's most exciting AJAX web applications. But actually writing client web applications that use this object can be tricky given restrictions impose

XMLHttpRequest对象解读

<!DOCTYPE html> <html> <body> <script> function reqListener () { console.log(this); console.log(this.getAllResponseHeaders()); console.log(this.responseText); } var params = "lorem=ipsum&name=binny"; var oReq = new XM

XMLHttpRequest对象

先抛出问题:我们如何使用JS脚本来发起一个http请求? 首先应该请楚,我们在使用浏览器浏览网页时,其实是浏览器帮我们发起一个又一个请求,唰唰唰~想象一下,当输入地址按下回车开始(其实在webkit的实现中,当我们输入地址时,浏览器已经在开始做地址匹配,DNS预查了..),一个GET请求便发起了,经过一系列的解析最终到达服务器(其中过程网上有很多版本,包括从键盘按下开始解释,这个不是我们现在的重点哈),服务器再返回我们想要的资源.那么,前端如何使用自己的代码发送请求,得到自己想要的数据呢?除了使