首页 > 编程 > JavaScript > 正文

JavaScript Base64 作为文件上传的实例代码解析

2019-11-19 17:35:45
字体:
来源:转载
供稿:网友

例如我们用某些 裁剪插件 得到的图片是 

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAkCAYAAABIdFAMAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAHhJREFUeNo8zjsOxCAMBFB/ KEAUFFR0Cbng3nQPw68ArZdAlOZppPFIBhH5EAB8b+Tlt9MYQ6i1BuqFaq1CKSVcxZ2Acs6406KUgpt5/ LCKuVgz5BDCSb13ZO99ZOdcZGvt4mJjzMVKqcha68iIePB86GAiOv8CDADlIUQBs7MD3wAAAABJRU5ErkJggg=="> 

这样的,那这样的文件怎样在from 表单上传(当然我是举例,大部分的裁剪插件都是有内置的)

首先需要 吧 base64 流转换成 blob 对象,文件对象都继承它。

函数如下:

function getBlobBydataURI(dataURI,type) {   var binary = atob(dataURI.split(',')[1]);   var array = [];   for(var i = 0; i < binary.length; i++) {     array.push(binary.charCodeAt(i));   }   return new Blob([new Uint8Array(array)], {type:type }); } 

那么剩下的就作为文件上传就ok

var $Blob= getBlobBydataURI(base64Data,'image/png'); var formData = new FormData(); formData.append("files", $Blob ,"file_"+Date.parse(new Date())+".png"); 

全部代码是这样子的:

<!DOCTYPE html> <html> <head>   <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>   <title>上传用例</title>   <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>   <script type="text/javascript">     /**      * 根据base64 内容 取得 bolb      *      * @param dataURI      * @returns Blob      */     function getBlobBydataURI(dataURI,type) {       var binary = atob(dataURI.split(',')[1]);       var array = [];       for(var i = 0; i < binary.length; i++) {         array.push(binary.charCodeAt(i));       }       return new Blob([new Uint8Array(array)], {type:type });     }     /**      * 上传      */     function upload(){       //base64 转 blob       var $Blob= getBlobBydataURI(document.getElementsByTagName("img")[0].currentSrc,'image/jpeg');       var formData = new FormData();       formData.append("files", $Blob ,"file_"+Date.parse(new Date())+".jpeg");       //组建XMLHttpRequest 上传文件       var request = new XMLHttpRequest();       //上传连接地址       request.open("POST", "www.xxx.com");       request.onreadystatechange=function()       {         if (request.readyState==4)         {           if(request.status==200){             console.log("上传成功");           }else{             console.log("上传失败,检查上传地址是否正确");           }         }       }       request.send(formData);     }   </script> </head> <body> <button onclick="upload()">上传测试</button> <br /> 上传的图片: <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAeAB4AAD/4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAEAAAAAAAD/2wBDABsSFBcUERsXFhceHBsgKEIrKCUlKFE6PTBCYFVlZF9VXVtqeJmBanGQc1tdhbWGkJ6jq62rZ4C8ybqmx5moq6T/wAALCABQADEBAREA/8QAGgAAAwADAQAAAAAAAAAAAAAAAwQFAAECBv/EADQQAAIBAwMCAgcGBwAAAAAAAAECAwAEEQUSITFBE1EVImFxgaHwBiMykbHRFEJDcoKiwf/aAAgBAQAAPwD0tc5AGc1MuftBYW7FfEMrDtGM/Ohp9pbNmUNHOgbo7qAP1p60vra8z4E6uR1HcfCmVrKHc3EdrBJNI2FUZNSIreXWR41w7xW38kSnBYebe/686o2ljaWwPgQxqemRyT8aO8UTqVeJWHkQKlzaHDuaS0Y28/VSh9X3EeX17KNpWoPc+Lb3KBLiDhwOhHmKo5qPqY9Iajb6eM+Go8aX2jsK71dmhW2tIcxrcSCNivUKeuKnw2mnyXc1rbJcRzxAnxd/UijwajdXkVlbrIqSXCsXkA5wCRx7eDQ7i4i024KxXV3PMuGKytlD8vLP5UfUHCXunaimVEuEYDqQeRVmpthg61qjHqPDH+p/anL+0F1Eu1gskbB42Izhh0pA2mqSho3a1h38PLGvrMK3Ppj24tpbHBktl2hWON47/Hmg3FnPduxbThbyyDa0xlDBR34HsouvRCLSIYlOdjoo+FP+tU6+b0Zq63pH3E6iOU4/Cex+VWI5FkUMrAg9CO9BvEnlgZLaURy54YjOKniy1sHPpKM/4D9qqqWCgOctgZ46mpN4w1HWLe1TlLY+LIfb2FWKHPBHcxtHKgZWGCCKi2y3FnqbWdlJ40CgM6yf0xnoD9frV+sqPrt1eWsG6EbIzgNL3XnsPr/tOaXZQWduFhbfv9ZnPVj503trrApeG0hgllkiTa0xzIck5P0TTGKzFDmhjnieKRdyOMMOmayGJIYkijXaqDCjOcCi1ql5ruGGQRt4hcjdhI2fj4A1w2oQqMsJ1Hm1vIB+eKJLdRQuElbYCMh24X3Z86WXWLJtv3yDchfl14x2PPWmra4juYEmiIKsM9c49nvo9aqXdyPHf+LGWXbHsObeSQdc8YwPnS5vnvrdgk6uN2DttJOx8wTinJ3dZFmhuCrSIQscqHacc5x1HGaQWYiaFI2UoYWCsLSX8ORwBu5B8/3qrpxJs4zuRlx6hQEAr24PNN1qlXhlnkKykCDPCLzv/uPl7Pn2ocVm8UZKSBJd7MGAyCCxIBHfrW/4eaa4Es2xQIigCknDE8noOwFci0mjeAwyIPCgMW5lJycr2yPI96ZtIBbWscAbdsUDPnR6/9k=" /> </body> </html> 

以上所述是小编给大家介绍的JavaScript Base64 作为文件上传的实例代码解析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表