1.file文件轉換爲base64,獲得base64格式圖片數組
var reader = new FileReader(); reader.readAsDataURL(this.files[0]); reader.onload = function(){ console.log(reader.result); //獲取到base64格式圖片 };
2.base64轉換爲file測試
function dataURLtoFile(dataurl, filename) {//將base64轉換爲文件 var arr = dataurl.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], filename, {type:mime}); }
測試案例:this
<input type="file" name="" id="imgfile"> <script> var base64Img = ''; document.getElementById('imgfile').onchange = function(){ var fileReader = new FileReader(); fileReader.readAsDataURL(this.files[0]); fileReader.onload = function(){ base64Img = fileReader.result; console.log(dataURLtoFile(base64Img,'img11')) } } function dataURLtoFile(dataurl, filename) {//將base64轉換爲文件 var arr = dataurl.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], filename, {type:mime}); } </script>
3.base64轉換爲blob流url
function dataURItoBlob(base64Data) { //console.log(base64Data);//data:image/png;base64, var byteString; if(base64Data.split(',')[0].indexOf('base64') >= 0) byteString = atob(base64Data.split(',')[1]);//base64 解碼 else{ byteString = unescape(base64Data.split(',')[1]); } var mimeString = base64Data.split(',')[0].split(':')[1].split(';')[0];//mime類型 -- image/png // var arrayBuffer = new ArrayBuffer(byteString.length); //建立緩衝數組 // var ia = new Uint8Array(arrayBuffer);//建立視圖 var ia = new Uint8Array(byteString.length);//建立視圖 for(var i = 0; i < byteString.length; i++) { ia[i] = byteString.charCodeAt(i); } var blob = new Blob([ia], { type: mimeString }); return blob; }
4.blob流轉換爲base64spa
function blobToDataURI(blob, callback) { var reader = new FileReader(); reader.readAsDataURL(blob); reader.onload = function (e) { callback(e.target.result); } }
測試案例,可直接複製運行code
<input type="file" id="imgfile"> <\img src="" id="img" alt=""> //這裏圖片老是轉義,暫且這麼寫,知道是img就行 <\img src="" id="img2" alt=""> <script> document.getElementById('imgfile').onchange = function(){ reads(this.files[0],function(base64Data){ //獲取圖片的base64格式,顯示 document.getElementById("img").src= reader.result; var blob = dataURItoBlob(reader.result); //轉換爲blob格式 blobToDataURI(blob,function(result){ //blob格式再轉換爲base64格式 document.getElementById('img2').src = result; }) }); } function reads(_file,callback){ var reader = new FileReader(); reader.readAsDataURL(_file); reader.onload = function(){ callback(reader.result); }; } function dataURItoBlob(base64Data) { //console.log(base64Data);//data:image/png;base64, var byteString; if(base64Data.split(',')[0].indexOf('base64') >= 0) byteString = atob(base64Data.split(',')[1]);//base64 解碼 else{ byteString = unescape(base64Data.split(',')[1]); } var mimeString = base64Data.split(',')[0].split(':')[1].split(';')[0];//mime類型 -- image/png // var arrayBuffer = new ArrayBuffer(byteString.length); //建立緩衝數組 // var ia = new Uint8Array(arrayBuffer);//建立視圖 var ia = new Uint8Array(byteString.length);//建立視圖 for(var i = 0; i < byteString.length; i++) { ia[i] = byteString.charCodeAt(i); } var blob = new Blob([ia], { type: mimeString }); return blob; } function blobToDataURI(blob, callback) { var reader = new FileReader(); reader.readAsDataURL(blob); reader.onload = function (e) { callback(e.target.result); } } </script>