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

上传图片预览

时间:2023-04-05 19:31:55 HTML5

htmljsconstbody=document.bodyfunctionshow(src){constimg=newImage()img.onload=function(){body.append(img)}img.src=src}/***@description:两个预览选项*@description:1.通过FileReader实例的readAsDataURL方法读取File或Blob对象为DataURL(即base64)*@description:2、通过URL的静态方法readAsDataURL方法将File或Blob对象读入ObjectURL(图片的地址)*@param{type}file*@return:*/functiongetSrc(file){//constreader=newFileReader()//reader.onload=function(){//show(this.result)//}//reader.readAsDataURL(file)constobjURL=URL.createObjectURL(file)show(objURL)}functionchange(target){constfile=target.files[0]getSrc(文件)}