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

【html+nativejs】如何将input标签转为base64数据,上传多张图片后返回

时间:2023-04-02 16:48:42 HTML

前台总结,最近帮后端同事解决了一个问题,他需要在上传多张选中的图片后,获取每张图片的base64数据。话不多说,我们直接上代码!页面部分(页面需要引入jq才能正常运行)test

逻辑部分$(function(){constimgJson={arr:[]}/***@description将文件转成base64data*@param{Object}file-文件对象*/functionchangeFileIntoBase64(file){returnnewPromise((resolve,reject)=>{constfr=newFileReader();fr.readAsDataURL(file);fr.onload=(result)=>{constbase64Str=result.currentTarget.result;resolve(base64Str);};});}/***@description初始化图像数据*/asyncfunctioninitImgArr(){constfileLength=$("#imageButton")[0].files.length;letarr=[];for(leti=0;i{constfileLength=$("#imageButton")[0].files.length;letarr=[];for(leti=0;i{initImgArr()});});输入结果如下图,目前支持点击获取或者自动生成数组赋值给指定的对象,如果大家有更好的解决方案欢迎留言。。。就这样。。。