﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-paulwong-随笔分类-HTML5</title><link>http://www.blogjava.net/paulwong/category/53697.html</link><description /><language>zh-cn</language><lastBuildDate>Sat, 08 Aug 2015 08:21:54 GMT</lastBuildDate><pubDate>Sat, 08 Aug 2015 08:21:54 GMT</pubDate><ttl>60</ttl><item><title>XMLHttpRequest Level 2 使用指南</title><link>http://www.blogjava.net/paulwong/archive/2015/08/07/426654.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Fri, 07 Aug 2015 04:24:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2015/08/07/426654.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/426654.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2015/08/07/426654.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/426654.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/426654.html</trackback:ping><description><![CDATA[<div id="share_button" style="margin: 0px; padding-top: 1em; padding-right: 2em; list-style-type: none; border: none; color: #111111; font-family: Georgia, serif; font-size: 12px; letter-spacing: -0.119999997317791px; line-height: 21.6000003814697px; word-spacing: 2.40000009536743px; float: right; background-color: #f5f5d5;"><a href="http://www.bshare.cn/share" style="margin: 0px; padding: 0px; list-style-type: none; text-decoration: none; border: none; color: #556677;">分享按钮</a></div><div style="margin: 1.5em auto 2em; list-style-type: none; border: none; color: #111111; line-height: 0.8; font-family: Georgia, serif; font-size: 12px; letter-spacing: -0.119999997317791px; word-spacing: 2.40000009536743px; background-color: #f5f5d5;"><p author"="" style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; color: #556677; font-size: 1.6em; line-height: 28.7999992370605px;">作者：&nbsp;<a url"="" href="http://www.ruanyifeng.com/" style="margin: 0px; padding: 0px; list-style-type: none; text-decoration: none; border: none; color: #556677;">阮一峰</a></p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; color: #556677; font-size: 1.6em; line-height: 28.7999992370605px;">日期：&nbsp;<a href="http://www.ruanyifeng.com/blog/2012/09/" style="margin: 0px; padding: 0px; list-style-type: none; text-decoration: none; border: none; color: #556677;">2012年9月 8日</a></p></div><div entry-content"="" id="main-content" style="margin: 0px; list-style-type: none; border: none; color: #111111; font-family: Georgia, serif; font-size: 12px; letter-spacing: -0.119999997317791px; line-height: 21.6000003814697px; word-spacing: 2.40000009536743px; background-color: #f5f5d5;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;"><a href="http://en.wikipedia.org/wiki/XMLHttpRequest" target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: #112233;">XMLHttpRequest</a>是一个浏览器接口，使得Javascript可以进行HTTP(S)通信。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">最早，微软在IE 5引进了这个接口。因为它太有用，其他浏览器也模仿部署了，ajax操作因此得以诞生。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">但是，这个接口一直没有标准化，每家浏览器的实现或多或少有点不同。HTML 5的概念形成后，W3C开始考虑标准化这个接口。2008年2月，就提出了<a href="http://dev.w3.org/2006/webapi/XMLHttpRequest-2/" target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: #112233;">XMLHttpRequest Level 2</a>&nbsp;草案。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">这个XMLHttpRequest的新版本，提出了很多有用的新功能，将大大推动互联网革新。本文就对这个新版本进行详细介绍。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;"><img src="http://image.beekka.com/blog/201209/bg2012090801.png" style="margin: 0px; padding: 0px; list-style-type: none; border: 0.3em solid #e0dfcc; border-radius: 1em;"  alt="" /></p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;"><span style="font-weight: 800;">一、老版本的XMLHttpRequest对象</span></p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">在介绍新版本之前，我们先回顾一下老版本的用法。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">首先，新建一个XMLHttpRequest的实例。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　var xhr = new XMLHttpRequest();</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">然后，向远程主机发出一个HTTP请求。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　xhr.open('GET', 'example.php');</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　xhr.send();</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">接着，就等待远程主机做出回应。这时需要监控XMLHttpRequest对象的状态变化，指定回调函数。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　xhr.onreadystatechange = function(){</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　　　if ( xhr.readyState == 4 &amp;&amp; xhr.status == 200 ) {</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　　　　　alert( xhr.responseText );</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　　　} else {</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　　　　　alert( xhr.statusText );</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　　　}</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　};</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">上面的代码包含了老版本XMLHttpRequest对象的主要属性：</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* xhr.readyState：XMLHttpRequest对象的状态，等于4表示数据已经接收完毕。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* xhr.status：服务器返回的状态码，等于200表示一切正常。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* xhr.responseText：服务器返回的文本数据</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* xhr.responseXML：服务器返回的XML格式的数据</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* xhr.statusText：服务器返回的状态文本。</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;"><span style="font-weight: 800;">二、老版本的缺点</span></p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">老版本的XMLHttpRequest对象有以下几个缺点：</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* 只支持文本数据的传送，无法用来读取和上传二进制文件。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* 传送和接收数据时，没有进度信息，只能提示有没有完成。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* 受到<a href="http://www.w3.org/Security/wiki/Same_Origin_Policy" target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: #112233;">"同域限制"</a>（Same Origin Policy），只能向同一域名的服务器请求数据。</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;"><span style="font-weight: 800;">三、新版本的功能</span></p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">新版本的XMLHttpRequest对象，针对老版本的缺点，做出了大幅改进。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* 可以设置HTTP请求的时限。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* 可以使用FormData对象管理表单数据。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* 可以上传文件。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* 可以请求不同域名下的数据（跨域请求）。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* 可以获取服务器端的二进制数据。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* 可以获得数据传输的进度信息。</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">下面，我就一一介绍这些新功能。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;"><span style="font-weight: 800;">四、HTTP请求的时限</span></p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">有时，ajax操作很耗时，而且无法预知要花多少时间。如果网速很慢，用户可能要等很久。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">新版本的XMLHttpRequest对象，增加了timeout属性，可以设置HTTP请求的时限。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　xhr.timeout = 3000;</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">上面的语句，将最长等待时间设为3000毫秒。过了这个时限，就自动停止HTTP请求。与之配套的还有一个timeout事件，用来指定回调函数。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　xhr.ontimeout = function(event){</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　　　alert('请求超时！');</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　}</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">目前，Opera、Firefox和IE 10支持该属性，IE 8和IE 9的这个属性属于XDomainRequest对象，而Chrome和Safari还不支持。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;"><span style="font-weight: 800;">五、FormData对象</span></p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">ajax操作往往用来传递表单数据。为了方便表单处理，HTML 5新增了一个FormData对象，可以模拟表单。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">首先，新建一个FormData对象。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　var formData = new FormData();</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">然后，为它添加表单项。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　formData.append('username', '张三');</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　formData.append('id', 123456);</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">最后，直接传送这个FormData对象。这与提交网页表单的效果，完全一样。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　xhr.send(formData);</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">FormData对象也可以用来获取网页表单的值。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　var form = document.getElementById('myform');</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　var formData = new FormData(form);</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　formData.append('secret', '123456'); // 添加一个表单项</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　xhr.open('POST', form.action);</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　xhr.send(formData);</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;"><span style="font-weight: 800;">六、上传文件</span></p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">新版XMLHttpRequest对象，不仅可以发送文本信息，还可以上传文件。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">假定files是一个"选择文件"的表单元素（input[type="file"]），我们将它装入FormData对象。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　var formData = new FormData();</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　for (var i = 0; i &lt; files.length;i++) {</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　　　formData.append('files[]', files[i]);</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　}</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">然后，发送这个FormData对象。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　xhr.send(formData);</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;"><span style="font-weight: 800;">七、跨域资源共享（CORS）</span></p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">新版本的XMLHttpRequest对象，可以向不同域名的服务器发出HTTP请求。这叫做<a href="http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing" target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: #112233;">"跨域资源共享"</a>（Cross-origin resource sharing，简称CORS）。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">使用"跨域资源共享"的前提，是浏览器必须支持这个功能，而且服务器端必须同意这种"跨域"。如果能够满足上面的条件，则代码的写法与不跨域的请求完全一样。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　xhr.open('GET', 'http://other.server/and/path/to/script');</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">目前，除了IE 8和IE 9，主流浏览器都支持CORS，IE 10也将支持这个功能。服务器端的设置，请参考<a href="https://developer.mozilla.org/en-US/docs/Server-Side_Access_Control" target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: #112233;">《Server-Side Access Control》</a>。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;"><span style="font-weight: 800;">八、接收二进制数据（方法A：改写MIMEType）</span></p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">老版本的XMLHttpRequest对象，只能从服务器取回文本数据（否则它的名字就不用XML起首了），新版则可以取回二进制数据。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">这里又分成两种做法。较老的做法是改写数据的MIMEType，将服务器返回的二进制数据伪装成文本数据，并且告诉浏览器这是用户自定义的字符集。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　xhr.overrideMimeType("text/plain; charset=x-user-defined");</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">然后，用responseText属性接收服务器返回的二进制数据。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　var binStr = xhr.responseText;</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">由于这时，浏览器把它当做文本数据，所以还必须再一个个字节地还原成二进制数据。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　for (var i = 0, len = binStr.length; i &lt; len; ++i) {</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　　　var c = binStr.charCodeAt(i);</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　　　var byte = c &amp; 0xff;</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　}</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">最后一行的位运算"c &amp; 0xff"，表示在每个字符的两个字节之中，只保留后一个字节，将前一个字节扔掉。原因是浏览器解读字符的时候，会把字符自动<a href="http://web.archive.org/web/20080821092906/http://mgran.blogspot.com/2006/08/downloading-binary-streams-with.html" target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: #112233;">解读</a>成Unicode的0xF700-0xF7ff区段。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;"><span style="font-weight: 800;">八、接收二进制数据（方法B：responseType属性）</span></p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">从服务器取回二进制数据，较新的方法是使用新增的responseType属性。如果服务器返回文本数据，这个属性的值是"TEXT"，这是默认值。较新的浏览器还支持其他值，也就是说，可以接收其他格式的数据。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">你可以把responseType设为blob，表示服务器传回的是二进制对象。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　var xhr = new XMLHttpRequest();</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　xhr.open('GET', '/path/to/image.png');</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　xhr.responseType = 'blob';</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">接收数据的时候，用浏览器自带的Blob对象即可。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　var blob = new Blob([xhr.response], {type: 'image/png'});</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">注意，是读取xhr.response，而不是xhr.responseText。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">你还可以将responseType设为arraybuffer，把二进制数据装在一个数组里。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　var xhr = new XMLHttpRequest();</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　xhr.open('GET', '/path/to/image.png');</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　xhr.responseType = "arraybuffer";</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">接收数据的时候，需要遍历这个数组。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　var arrayBuffer = xhr.response;</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　if (arrayBuffer) {</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　　　var byteArray = new Uint8Array(arrayBuffer);</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　　　for (var i = 0; i &lt; byteArray.byteLength; i++) {</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　　　　　// do something</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　　　}<br />　　}</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">更详细的讨论，请看<a href="https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Sending_and_Receiving_Binary_Data" target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: #112233;">Sending and Receiving Binary Data</a>。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;"><span style="font-weight: 800;">九、进度信息</span></p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">新版本的XMLHttpRequest对象，传送数据的时候，有一个progress事件，用来返回进度信息。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">它分成上传和下载两种情况。下载的progress事件属于XMLHttpRequest对象，上传的progress事件属于XMLHttpRequest.upload对象。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">我们先定义progress事件的回调函数。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　xhr.onprogress = updateProgress;</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　xhr.upload.onprogress = updateProgress;</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">然后，在回调函数里面，使用这个事件的一些属性。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　function updateProgress(event) {</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　　　if (event.lengthComputable) {</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　　　　　var percentComplete = event.loaded / event.total;</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　　　}</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　}</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">上面的代码中，event.total是需要传输的总字节，event.loaded是已经传输的字节。如果event.lengthComputable不为真，则event.total等于0。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">与progress事件相关的，还有其他五个事件，可以分别指定回调函数：</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border-width: 0.3em; border-color: #e0dfcc; color: #111111; border-radius: 1em; font-family: Consolas, Monaco, 'Andale Mono', monospace; text-shadow: white 0px 1px; overflow: auto; background-color: #f5f2f0;"><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* load事件：传输成功完成。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* abort事件：传输被用户取消。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* error事件：传输中出现错误。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* loadstart事件：传输开始。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　* loadEnd事件：传输结束，但是不知道成功还是失败。</p></blockquote><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;"><span style="font-weight: 800;">十、阅读材料</span></p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　1.&nbsp;<a href="http://dev.opera.com/articles/view/xhr2/" target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: #112233;">Introduction to XMLHttpRequest Level 2</a>： 新功能的综合介绍。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　2.&nbsp;<a href="http://www.html5rocks.com/en/tutorials/file/xhr2/" target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: #112233;">New Tricks in XMLHttpRequest 2</a>：一些用法的介绍。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　3.&nbsp;<a href="https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest" target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: #112233;">Using XMLHttpRequest</a>：一些高级用法，主要针对Firefox浏览器。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　4.&nbsp;<a href="https://developer.mozilla.org/en-US/docs/HTTP_access_control" target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: #112233;">HTTP Access Control</a>：CORS综述。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　5.&nbsp;<a href="http://dev.opera.com/articles/view/dom-access-control-using-cross-origin-resource-sharing/" target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: #112233;">DOM access control using cross-origin resource sharing</a>：CORS的9种HTTP头信息</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　6.&nbsp;<a href="https://developer.mozilla.org/en-US/docs/Server-Side_Access_Control" target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: #112233;">Server-Side Access Control</a>：服务器端CORS设置。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">　　7.&nbsp;<a href="http://enable-cors.org/" target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: #112233;">Enable CORS</a>：服务端CORS设置。</p><p style="margin: 1em 0px 0px 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.7999992370605px;">（完）</p></div><img src ="http://www.blogjava.net/paulwong/aggbug/426654.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/paulwong/" target="_blank">paulwong</a> 2015-08-07 12:24 <a href="http://www.blogjava.net/paulwong/archive/2015/08/07/426654.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>理解DOMString、Document、FormData、Blob、File、ArrayBuffer数据类型</title><link>http://www.blogjava.net/paulwong/archive/2015/08/07/426653.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Fri, 07 Aug 2015 04:08:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2015/08/07/426653.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/426653.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2015/08/07/426653.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/426653.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/426653.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 一、XMLHttpRequest 2.0的家臣们我大学那会儿，一个称为Ajax的东西对前端行业造成了深远影响，不仅是JS语言，而包括前端地位、职位兴起以及工作分工等。抛开IE6浏览器不谈，其他浏览器的Ajax实际上都是借助XMLHttpRequest实现的。然后，好多年过去了，XMLHttpRequest带着两位家臣，DOMString和Document数据类型攻城略地，几乎一统天下。然时代是发展...&nbsp;&nbsp;<a href='http://www.blogjava.net/paulwong/archive/2015/08/07/426653.html'>阅读全文</a><img src ="http://www.blogjava.net/paulwong/aggbug/426653.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/paulwong/" target="_blank">paulwong</a> 2015-08-07 12:08 <a href="http://www.blogjava.net/paulwong/archive/2015/08/07/426653.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>AJAX下载+监控进度+保存文件</title><link>http://www.blogjava.net/paulwong/archive/2015/08/06/426641.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Thu, 06 Aug 2015 11:17:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2015/08/06/426641.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/426641.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2015/08/06/426641.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/426641.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/426641.html</trackback:ping><description><![CDATA[全程用AJAX下载文件，并显示下载进度，之后保存文件。<br /><br />HTML5文件：<br /><br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">&lt;!</span><span style="color: #FF00FF; ">DOCTYPE&nbsp;html</span><span style="color: #0000FF; ">&gt;</span><br /><span style="color: #0000FF; ">&lt;</span><span style="color: #800000; ">html</span><span style="color: #0000FF; ">&gt;</span><br /><span style="color: #0000FF; ">&lt;</span><span style="color: #800000; ">head</span><span style="color: #0000FF; ">&gt;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">&lt;</span><span style="color: #800000; ">title</span><span style="color: #0000FF; ">&gt;</span>XMLHttpRequest&nbsp;Download&nbsp;Progress<span style="color: #0000FF; ">&lt;/</span><span style="color: #800000; ">title</span><span style="color: #0000FF; ">&gt;</span><br /><span style="color: #0000FF; ">&lt;/</span><span style="color: #800000; ">head</span><span style="color: #0000FF; ">&gt;</span><br /><span style="color: #0000FF; ">&lt;</span><span style="color: #800000; ">body</span><span style="color: #0000FF; ">&gt;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">&lt;</span><span style="color: #800000; ">progress&nbsp;</span><span style="color: #FF0000; ">id</span><span style="color: #0000FF; ">="p"</span><span style="color: #0000FF; ">&gt;&lt;/</span><span style="color: #800000; ">progress</span><span style="color: #0000FF; ">&gt;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">&lt;</span><span style="color: #800000; ">input&nbsp;</span><span style="color: #FF0000; ">type</span><span style="color: #0000FF; ">="button"</span><span style="color: #FF0000; ">&nbsp;onclick</span><span style="color: #0000FF; ">="downloadAndSave();"</span><span style="color: #FF0000; ">&nbsp;value</span><span style="color: #0000FF; ">="Download"</span><span style="color: #0000FF; ">/&gt;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">&lt;</span><span style="color: #800000; ">script</span><span style="color: #0000FF; ">&gt;</span><span style="background-color: #f5f5f5;"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="background-color: #F5F5F5; color: #0000FF; ">function</span><span style="background-color: #f5f5f5;">&nbsp;downloadAndSave()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="background-color: #F5F5F5; color: #0000FF; ">var</span><span style="background-color: #f5f5f5;">&nbsp;progressBar&nbsp;</span><span style="background-color: #f5f5f5;">=</span><span style="background-color: #f5f5f5;">&nbsp;document.getElementById('p'),&nbsp;xhr&nbsp;</span><span style="background-color: #f5f5f5;">=</span><span style="background-color: #f5f5f5;">&nbsp;</span><span style="background-color: #F5F5F5; color: #0000FF; ">new</span><span style="background-color: #f5f5f5;">&nbsp;XMLHttpRequest();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xhr.open('GET',&nbsp;'</span><span style="background-color: #f5f5f5;">2</span><span style="background-color: #f5f5f5;">');<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xhr.responseType&nbsp;</span><span style="background-color: #f5f5f5;">=</span><span style="background-color: #f5f5f5;">&nbsp;</span><span style="background-color: #f5f5f5;">"</span><span style="background-color: #f5f5f5;">arraybuffer</span><span style="background-color: #f5f5f5;">"</span><span style="background-color: #f5f5f5;">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xhr.onprogress&nbsp;</span><span style="background-color: #f5f5f5;">=</span><span style="background-color: #f5f5f5;">&nbsp;</span><span style="background-color: #F5F5F5; color: #0000FF; ">function</span><span style="background-color: #f5f5f5;">(event)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="background-color: #F5F5F5; color: #0000FF; ">if</span><span style="background-color: #f5f5f5;">(event.lengthComputable)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;progressBar.max&nbsp;</span><span style="background-color: #f5f5f5;">=</span><span style="background-color: #f5f5f5;">&nbsp;event.total;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;progressBar.value&nbsp;</span><span style="background-color: #f5f5f5;">=</span><span style="background-color: #f5f5f5;">&nbsp;event.loaded;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xhr.onloadend&nbsp;</span><span style="background-color: #f5f5f5;">=</span><span style="background-color: #f5f5f5;">&nbsp;</span><span style="background-color: #F5F5F5; color: #0000FF; ">function</span><span style="background-color: #f5f5f5;">(event)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;progressBar.value&nbsp;</span><span style="background-color: #f5f5f5;">=</span><span style="background-color: #f5f5f5;">&nbsp;event.loaded;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;saveByeToFile('</span><span style="background-color: #f5f5f5;">2</span><span style="background-color: #f5f5f5;">',&nbsp;xhr.response);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xhr.send();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="background-color: #F5F5F5; color: #0000FF; ">function</span><span style="background-color: #f5f5f5;">&nbsp;saveByeToFile(name,&nbsp;arrayBuffer)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="background-color: #F5F5F5; color: #0000FF; ">var</span><span style="background-color: #f5f5f5;">&nbsp;byteArray&nbsp;</span><span style="background-color: #f5f5f5;">=</span><span style="background-color: #f5f5f5;">&nbsp;</span><span style="background-color: #F5F5F5; color: #0000FF; ">new</span><span style="background-color: #f5f5f5;">&nbsp;Uint8Array(arrayBuffer);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="background-color: #F5F5F5; color: #0000FF; ">var</span><span style="background-color: #f5f5f5;">&nbsp;a&nbsp;</span><span style="background-color: #f5f5f5;">=</span><span style="background-color: #f5f5f5;">&nbsp;window.document.createElement('a');<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.href&nbsp;</span><span style="background-color: #f5f5f5;">=</span><span style="background-color: #f5f5f5;">&nbsp;window.URL.createObjectURL(</span><span style="background-color: #F5F5F5; color: #0000FF; ">new</span><span style="background-color: #f5f5f5;">&nbsp;Blob([&nbsp;byteArray&nbsp;],&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type&nbsp;:&nbsp;'application</span><span style="background-color: #f5f5f5;">/</span><span style="background-color: #f5f5f5;">octet</span><span style="background-color: #f5f5f5;">-</span><span style="background-color: #f5f5f5;">stream'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.download&nbsp;</span><span style="background-color: #f5f5f5;">=</span><span style="background-color: #f5f5f5;">&nbsp;name;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="background-color: #F5F5F5; color: #008000; ">//</span><span style="background-color: #F5F5F5; color: #008000; ">&nbsp;Append&nbsp;anchor&nbsp;to&nbsp;body.</span><span style="background-color: #F5F5F5; color: #008000; "><br /></span><span style="background-color: #f5f5f5;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.body.appendChild(a)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.click();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="background-color: #F5F5F5; color: #008000; ">//</span><span style="background-color: #F5F5F5; color: #008000; ">&nbsp;Remove&nbsp;anchor&nbsp;from&nbsp;body</span><span style="background-color: #F5F5F5; color: #008000; "><br /></span><span style="background-color: #f5f5f5;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.body.removeChild(a)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">&lt;/</span><span style="color: #800000; ">script</span><span style="color: #0000FF; ">&gt;</span><br /><span style="color: #0000FF; ">&lt;/</span><span style="color: #800000; ">body</span><span style="color: #0000FF; ">&gt;</span><br /><span style="color: #0000FF; ">&lt;</span><span style="color: #800000; ">html</span><span style="color: #0000FF; ">&gt;</span></div><img src ="http://www.blogjava.net/paulwong/aggbug/426641.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/paulwong/" target="_blank">paulwong</a> 2015-08-06 19:17 <a href="http://www.blogjava.net/paulwong/archive/2015/08/06/426641.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HTML5资源</title><link>http://www.blogjava.net/paulwong/archive/2013/06/30/401085.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Sun, 30 Jun 2013 11:27:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2013/06/30/401085.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/401085.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2013/06/30/401085.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/401085.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/401085.html</trackback:ping><description><![CDATA[HTML5 Canvas初体验之绘图基础<br />
<a href="http://www.e800.com.cn/articles/2012/0315/504978.shtml" target="_blank">http://www.e800.com.cn/articles/2012/0315/504978.shtml</a><br />
<br />
<br />
HTML5 Canvas Bezier Curve Tutorial<br />
<a href="http://www.html5canvastutorials.com/tutorials/html5-canvas-bezier-curves/" target="_blank">http://www.html5canvastutorials.com/tutorials/html5-canvas-bezier-curves/</a><br />
<br />
<br />
HTML 5 Canvas<br />
<a href="http://www.w3school.com.cn/html5/html_5_canvas.asp" target="_blank">http://www.w3school.com.cn/html5/html_5_canvas.asp</a><br />
<br />
<br />RDF 简介<br />
<a href="http://www.w3school.com.cn/rdf/rdf_intro.asp" target="_blank">http://www.w3school.com.cn/rdf/rdf_intro.asp</a><br />
<a href="http://www.w3schools.com/rdf/rdf_intro.asp" target="_blank">http://www.w3schools.com/rdf/rdf_intro.asp</a><br />
<br />
<br />手机界面框架<br /><a href="http://ionicframework.com/" target="_blank">http://ionicframework.com/</a><br /><br /><br />
<br />
<br />
<br />
<br />
<img src ="http://www.blogjava.net/paulwong/aggbug/401085.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/paulwong/" target="_blank">paulwong</a> 2013-06-30 19:27 <a href="http://www.blogjava.net/paulwong/archive/2013/06/30/401085.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>