廢話不多說,直接上代碼
<!DOCTYPE html>
<html>
<head>
<title>本地圖片轉base64</title>
</head>
<body>
<input type="file" id="imgfile" method="post" enctype="multipart/form-data">
<button type="button" onClick="ToBase64();">點我</button>
</body>
<script type="text/javascript">
function ToBase64() {
debugger
/*轉換base64*/
var img = document.getElementById('imgfile')
var imgFile = new FileReader();
imgFile.readAsDataURL(img.files[0]);
imgFile.onload = function () {
var imgData = this.result; //base64數據
console.log(imgData);
}
}
</script>
</html>
豐富知識,一切源自於興趣