-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.html
94 lines (90 loc) · 2.92 KB
/
upload.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.img {
width: 200px;
height: 200px;
background-position: center center;
background-size: 100% 100%;
background-repeat: no-repeat;
margin: 30px;
}
#parent{
position: relative; /*子绝父相*/
}
#left {
position: absolute;
top: 0;
left: 0;
background-color: #f00;
width: 100px;
height: 500px;
}
#right {
position: absolute;
top: 0;
left: 100px; /*值大于等于#left的宽度*/
right: 0;
background-color: #0f0;
height: 500px;
}
</style>
</head>
<body>
<div id="parent">
<div id="left">左列定宽</div>
<div id="right">右列自适应</div>
</div>
<input type="file" id="imgInput" accept="image/*" multiple/>
</body>
<script>
let img = document.getElementById('img');
let input = document.getElementById('imgInput');
input.onchange = function(e) {
console.log(this, 'this')
console.log(e.target.files);
let p = Promise.resolve(previewImg(e.target.files[0]))
let d = [...e.target.files]
d.slice(1).forEach(item => {
p = p.then(previewImg(item))
});
return p;
// previewImg(item)
}
// var reader = new FileReader();
// 为什么在外界使用同一个报 DOMException: Failed to execute 'readAsDataURL' on 'FileReader': The object is already busy reading Blobs 都放在了 promise 里面
function previewImg (file){
return new Promise(function(res, rej){
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(event) {
let img = document.createElement('div');
img.className = 'img'
img.style.backgroundImage= 'url('+ event.target.result + ')';
document.body.appendChild(img);
console.log('res......', file)
reader.abort()
res()
}
})
}
// xhr.status 不是一定在 readyState 是 4 的时候执行才有 200 直言完成了某个阶段也是 200
var xhr= new XMLHttpRequest(),
method = "GET",
url = "https://developer.mozilla.org/";
console.log('xhr.readyState', xhr.readyState, xhr.status)
xhr.open(method, url, true);
console.log('xhr.readyState', xhr.readyState, xhr.status)
xhr.onreadystatechange = function () {
console.log('xhr.readyState', xhr.readyState, xhr.status)
if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
console.log(xhr.responseText)
}
}
xhr.send();
console.log('xhr.readyState', xhr.readyState, xhr.status)
</script>
</html>