<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>html5将图片转换base64进行上传、解决微信内置浏览器上传图片问题</title>
</head>
<body>
<script>
function readFile(obj){
var file = obj.files[0];
//判断类型是不是图片
if(!/image\/\w+/.test(file.type)){
alert("请确保文件为图像类型");
return false;
}
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(e){
alert(this.result); //就是base64
}
}
</script>
<input type="file" id="picFile" onchange="readFile(this)" />
</body>
</html>
扩展阅读:
html5将图片转换base64进行上传、解决微信内置浏览器上传图片问题
HTML5学习之FileReader接口
HTML5的FileReader接口封装 – FileReader.js