将本地图片转成base64

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input type="file" id="inp">
<img id="img">
<script>
document.getElementById("inp").onchange = function () {
var file = this.files[0];
var a = new FileReader();
a.onload = function (e) {
console.log(e);
var base64 = e.target.result;
document.getElementById("img").src = base64
}
a.error = function () {
console.log("失败");
}
a.readAsDataURL(file);
}
</script>
</body>
</html>
posted @ 2019-09-05 09:23  松歌  阅读(2537)  评论(0编辑  收藏  举报