<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
.imgshower {
width: 400px;
height: 300px;
border: 1px solid red;
display: flex;
align-items: center;
justify-content: center;
}
img {
width: 100%;
height: auto;
}
</style>
<body>
<div class="imgshower">
<img src="">
</div>
<input type="file" name="" id="file" value="" />
</body>
<script type="text/javascript">
var file = document.getElementById('file');
var image = document.querySelector("img");
file.onchange = function() {
var fileData = this.files[0];
var pettern = /^image/;
console.info(fileData.type)
if (!pettern.test(fileData.type)) {
alert("图片格式不正确");
return;
}
var reader = new FileReader();
reader.readAsDataURL(fileData);
reader.onload = function(e) {
console.log(e);
console.log(this.result);
image.setAttribute("src", this.result)
}
}
</script>
</html>
记录-JavaScript实现图片的异步上传
最新推荐文章于 2022-04-13 16:40:23 发布