/
test.html
58 lines (56 loc) · 1.33 KB
/
test.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>image upload demo</title>
<style>
.upload-btn {
display: inline-block;
padding: 0 15px;
line-height: 38px;
text-align: center;
font-size: 14px;
min-width: 50px;
border-radius: 4px;
background-color: #00a5e0;
color: #fff;
cursor: pointer;
}
.upload-btn:hover {
background-color: #00b4f5;
}
#imgInput {
position: absolute;
clip: rect(0, 0, 0, 0);
}
</style>
</head>
<body>
<form id="myForm">
<label class="upload-btn" for="imgInput">上传文件</label>
<input type="file" id="imgInput" accept="image/*">
<p class="file-name"></p>
<div id="preview"></div>
<button type="button" onclick="clear()">清除</button>
</form>
<script>
var imgInput = document.getElementById('imgInput');
var preview = document.getElementById('preview');
var fileName = document.getElementsByClassName('file-name')[0];
imgInput.addEventListener('change', function(e){
var html;
var file = e.target.files[0];
fileName.innerHTML = file.name;
var reader = new FileReader();
reader.onload = function(evt) {
html = '<img src="' + evt.target.result + '" />';
preview.innerHTML = html;
}
reader.readAsDataURL(file);
})
function clear() {
document.myForm.reset();
}
</script>
</body>
</html>