js中blob转string、string转blob、typeArray转blob、blob转TypeArray
将String字符串转换成Blob对象
var blob = new Blob(["Hello World!"], {
type: 'text/plain'
});
console.info(blob);
console.info(blob.slice(1, 3, 'text/plain'));
将TypeArray 转换成 Blob 对象
var array = new Uint16Array([97, 32, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]);
var blob = new Blob([array]);
var reader = new FileReader();
reader.readAsText(blob, 'utf-8');
reader.onload = function (e) {
console.info(reader.result);
}
ArrayBuffer转Blob
var buffer = new ArrayBuffer(32);
var blob = new Blob([buffer]);
将Blob对象转换成String字符串
var blob = new Blob(['中文字符串'], {
type: 'text/plain'
});
var reader = new FileReader();
reader.readAsText(blob, 'utf-8');
reader.onload = function (e) {
console.info(reader.result);
}
将Blob对象转换成ArrayBuffer
var blob = new Blob(['中文字符串'], {
type: 'text/plain'
});
var reader = new FileReader();
reader.readAsArrayBuffer(blob);
reader.onload = function (e) {
console.info(reader.result);
var buf = new Uint8Array(reader.result);
console.info(buf);
reader.readAsText(new Blob([buf]), 'utf-8');
reader.onload = function () {
console.info(reader.result);
};
var buf = new DataView(reader.result);
console.info(buf);
reader.readAsText(new Blob([buf]), 'utf-8');
reader.onload = function () {
console.info(reader.result);
};
}