两个数组比较:a.sort().toString() == b.sort().toString()
for循环内有异步方法时,需要闭包
JSON.parse(data)出错时(提示、[nodejs中]),可能是json本身有问题,空对象中加了空格。
xhr, blob, base64
window.URL.createObjectURL(blob): blob是 file和blob对象; 创建一个新的对象URL,该对象URL可以代表某一个指定的File
对象或Blob
对象.
用法:本地图片预览:
var xhr = new XMLHttpRequest(); xhr.open('GET', 'test.jpg', true); xhr.responseType = 'blob'; xhr.onload = function(e){ if(this.status == 200){ var blob = this.response; console.log(window.URL.createObjectURL(blob)); } } xhr.send(null);
注意:1、要见server;2、responseType='blob'; this.responseText:只有为''或'text'时才能看到;3、Blob {type: "image/jpeg", size: 76797, slice: function}
下载图片的另一种方式:
var xhr = new XMLHttpRequest(); xhr.open('GET', 'test.jpg', true); xhr.responseType = 'arraybuffer'; xhr.onload = function(e){ if(this.status == 200){ var uInt8Array = new Uint8Array(this.response); var i = uInt8Array.length; var binaryString = new Array(i); while(i--){ binaryString[i] = String.fromCharCode(uInt8Array[i]); } var data = binaryString.join(''); var base64 = window.btoa(data); console.log('data:image/png;base64,'+base64); } } xhr.send(null);
var uInt8Array = new Uint8Array(this.response); this.response是ArrayBuffer,在它基础上建了一个视图
binaryString[i] = String.fromCharCode(uInt8Array[i]); uInt8Array[i]:是unicode值(0-255),fromCharCode把unicode值转为字符串(Ù)。
var base64 = window.btoa(data); 把字符串编码为base64,解码时atob()
'data:image/png;base64,'+base64 可以访问图片。Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一
curl是利用URL语法在命令行方式下工作的开源文件传输工具:curl -v -r 100-200 http://localhost:3000/test.jpg > /dev/null: 检测server是否允许range request。express是支持的。
html状态码206:部分内容,区间请求range request。