134654865
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图片上传</title>
</head>
<body>
<img id="img" class="accounts-name" src="images/lihuazou.jpg">
<input type="file" id="imgInput" value='' class="pa y_file">
<script>
var img = document.getElementById("img"),
imgInput = document.getElementById("imgInput"),
fileSize = 500000;
function _ajax(_url) {
var XHR = createXMLHTTPXHRuest();
if (XHR) {
XHR.open("POST", _url, true);
XHR.setXHRuestHeader("Content-Type", "application/x-www-form-urlencoded; charset=gbk;");
XHR.send({
url: _url
});
XHR.onreadystatechange = function() {
if (XHR.readyState == 4) {
if (XHR.status == 200) {
//alert("success");
} else {
return false;
//alert("error");
}
}
}
}
}
imgInput.addEventListener("change", function() {
var File = this.files[0],
reader = new FileReader();
if (!this.files || !File) {
return false;
};
if (!/^image/g.test(File.type)) {
//alert("请选择图片格式");
return false;
};
if (File.size > fileSize) {
//alert("图片大小超过限制");
return false;
};
reader.onload = function(n) {
//_ajax(n.target.result);
img.src = n.target.result
};
reader.readAsDataURL(File);
});
</script>
</body>
</html>