对于文件的blob、base64、File的转化
64->File
function toFile(ndata) { // ndata为base64格式地址 let arr = ndata.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } return new File([u8arr], this.fileName, {type:mime}) }
blob->64
//**blob to dataURL** function blobToDataURL(blob, callback) { var a = new FileReader(); a.onload = function (e) { callback(e.target.result); } a.readAsDataURL(blob); }
原生请求blob
var ajax = new XMLHttpRequest(); ajax.onreadystatechange = function(){ if(ajax.readyState==4 && ajax.status==200){ console.log(ajax.response)//blob; blobToDataURL(ajax.response,(e)=>{ console.log(e)//base64 console.log(toFile(e))//Fille }) } } ajax.open("GET",url,true); ajax.responseType = "blob"//设置返回值为blob类型 ajax.send();