html实现图片上传的效果

 

 

 

 

<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>

 

  • 3
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值