forked from fudansswebfundamental/fdu-17ss-web-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestGetImage.html
57 lines (53 loc) · 1.41 KB
/
testGetImage.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.addBorder{
border:1px solid #ccc;
}
#imgDiv{
width:200px;
height:auto;
}
#imgContent{
width: 100%;
height:100%;
}
</style>
</head>
<body>
<form id="imgForm">
<input type="file" class="addBorder">
<br/>
<button type="button" onclick="loadImg()">获取图片</button>
</form>
<div class="addBorder" id="imgDiv">
<!--<img id="imgContent">-->
<img id="imgContent">
</div>
<!--<input type="file" name="file_upload" id="input">-->
<!--<audio id="audio" autoplay controls></audio>-->
<script src="js/jquery-3.3.1.js"></script>
<script>
function loadImg(){
//获取文件,html对象
let file = $("#imgForm").find("input")[0].files[0];
//创建读取文件的对象,读取文件之后所生成的对应文件的base64码
let reader = new FileReader();
//创建文件读取相关的变量
let imgFile;
//为文件读取成功设置事件
reader.onload = function(e) {
alert('文件读取完成');
imgFile = e.target.result;
console.log(imgFile);
$("#imgContent").attr('src', imgFile);
};
//正式读取文件
reader.readAsDataURL(file);
}
</script>
</body>
</html>