当前位置: 首页 > Web前端 > HTML

Util-前端Base64转File格式

时间:2023-03-28 17:57:58 HTML

exportfunctionbase64ToFile(base64Str){//转base64为blobconstdataURLtoBlob=function(dataurl){vararr=dataurl.split(','),mime=arr[0].match(/:(.*?);/)[1],bstr=atob(arr[1]),n=bstr.length,u8arr=newUint8Array(n);while(n--){u8arr[n]=bstr.charCodeAt(n);}returnnewBlob([u8arr],{type:mime});}//将blob转换为文件constblobToFile=function(theBlob,fileName){theBlob.lastModifiedDate=newDate();theBlob.name=文件名;returnnewwindow.File([theBlob],theBlob.name,{type:theBlob.type});}//调用varblob=dataURLtoBlob(base64Str);varfile=blobToFile(blob,Math.random().toString(36).substr(2));返回文件}