<html>
<head></head>
<style>
*{
padding: 0;
margin: 0 atuo;
}
.top{
height: 300px;
width: 1000px;
display: flex;
justify-content: space-between;
}
.left{
text-align: center;
width: 400px;
height: 300px;
border: 1px dashed #cccccc;
background: #efefef;
}
.right{
width: 590px;
height: 300px;
border: 1px dashed #cccccc;
background: white;
}
#imgDiv{
width: 100vw;
height: 100vh;
}
#imgDiv img{
width: 10vw;
height: 10vh;
}
</style>
<body>
<div class="top">
<div class="left">
<form method="get" enctype="multipart/form-data"> <img src="./subpicture.png" />
<label for="inputFile" class="button">
<div style="line-height: 40px; margin: auto; width: 200px;height: 40px;color: #FFFFFF;background: #00B7EF;border: none;border-radius: 3px;">点击选择文件</div>
</label>
<input type="file" id="inputFile" style="display: none; " multiple="multiple"></form>
</div>
<div class="right"></div>
</div>
<div style="width: 998px;height: 45px; background: #FFFFFF;border: 1px solid #CCCCCC;line-height: 45px;">
<span id="wenjian">选中0张文件 共0KB</span>
<label for="inputFile" class="button">
<span style="line-height: 40px; text-align: center; width: 100px;height: 40px; background: white;border: 1px solid #CCCCCC;border-radius: 3px;display: inline-block;">继续选择</span>
</label>
<input type="file" id="inputFile" style="display: none; " multiple="multiple">
<button style="border:none;background:#00B7EF ;color: white;width: 100px;height: 35px;" onclick="sub()">开始上传</button>
</div>
<div id="imgDiv"></div>
<script>
let imgarr = []
var file = document.getElementById("inputFile");
let imgDiv = document.getElementById("imgDiv");
let wenjian = document.getElementById("wenjian");
let size1
file.addEventListener("change", function(event){
imgarr = [...file.files]
let arr = imgarr.map(v => {
console.log(v.size,"size")
return v.size
})
arr = arr.reduce((total, num) => {
return total + num;
})
wenjian.innerText = `选中${imgarr.length}张文件 共${arr}KB`
})
function sub() {
for (var i = 0; i < file.files.length; i++) {
let reader = new FileReader();
var file1 = file.files[i];
reader.readAsDataURL(file1);
reader.onload = function(result) {
imgDiv.innerHTML += '<img src="' + reader.result + '" alt=""/>'
// console.log(reader.result)
}
}
console.log(file.files)
}
function read() {
for (var i = 0; i < file.files.length; i++) {
let reader = new FileReader();
var file1 = file.files[i];
reader.readAsDataURL(file1);
reader.onload = function(result) {
//reader对象的result属性存储流读取的数据
imgDiv.innerHTML += '<img src="' + reader.result + '" alt="" />'
console.log(reader.result) // 打印出转换后的base64
}
}
}
</script>
</body>
</html>