本地图片转base64

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function ToBase64() {
/*转换base64*/
var img = document.getElementById('upload')
var imgFile = new FileReader();
imgFile.readAsDataURL(img.files[0]);

imgFile.onload = function () {
var imgData = this.result; //base64数据
console.log(imgData);
}

}
</script>
<input type="file" name="" id="upload">
<button onclick="ToBase64()">to base64</button>
</body>
</html>

posted @ 2019-12-30 16:40  adolfvicto  阅读(375)  评论(0编辑  收藏  举报