上传照片并显示

上传一张照片并显示

<html>
<head>
	<meta charset="utf-8"/>
</head>
<body>
	<div style="border:2px dashed red;">
            <p>
                <input type="file" id="xdaTanFileImg" οnchange="xmTanUploadImg(this)" accept="image/*"/>
            </p>
            <img id="xmTanImg"/>
			<img 
        </div>
        <hr />
        <script type="text/javascript">            
            //判断浏览器是否支持FileReader接口
            if (typeof FileReader == 'undefined') {
                document.getElementById("xmTanDiv").InnerHTML = "<h1>当前浏览器不支持FileReader接口</h1>";
                //使选择控件不可操作
                document.getElementById("xdaTanFileImg").setAttribute("disabled", "disabled");
            }

            //选择图片,马上预览
            function xmTanUploadImg(obj) {
                var file = obj.files[0];
                
                console.log(obj);console.log(file);
                console.log("file.size = " + file.size);  //file.size 单位为byte

                var reader = new FileReader();

                //读取文件过程方法
                reader.onloadstart = function (e) {
                    console.log("开始读取....");
                }
                reader.onprogress = function (e) {
                    console.log("正在读取中....");
                }
                reader.onabort = function (e) {
                    console.log("中断读取....");
                }
                reader.onerror = function (e) {
                    console.log("读取异常....");
                }
                reader.onload = function (e) {
                    console.log("成功读取....");

                    var img = document.getElementById("xmTanImg");
					img.width = 300;
					img.height = 250;
                    img.src = e.target.result;
					
                    //或者 img.src = this.result;  //e.target == this
                }

                reader.readAsDataURL(file)
            }
        </script>
</body>

可以使用表格显示多张图片

<html>
<head>
	<meta charset="utf-8"/>
</head>
<body>
	<style>
		img{
			width:300px;
			max-width:100%;
			height:auto;
		}
	</style>
	<div style="border:2px dashed red;">
			
            <table border="1">
			<tr>
			<td>
				<p>
					<input type="file" id="file0" οnchange="xmTanUploadImg(this)" accept="image/*"/></br>
					<img id="xmTanImg_file0"/>
				</p>
			</td>
			<td>
				<p>
					<input type="file"  id="file1" οnchange="xmTanUploadImg(this)" accept="image/*"/></br>
					<img id="xmTanImg_file1"/>
				</p>
			</td>
			<td>
				<p>
					<input type="file"  id="file2" οnchange="xmTanUploadImg(this)" accept="image/*"/></br>
					<img id="xmTanImg_file2"/>
				</p>
			</td>
			</tr>
			<tr>
			<td>
				<p>
					<input type="file"  id="file3" οnchange="xmTanUploadImg(this)" accept="image/*"/></br>
					<img id="xmTanImg_file3"/>
				</p>
			</td>
			<td>
				<p>
					<input type="file"  id="file4" οnchange="xmTanUploadImg(this)" accept="image/*"/></br>
					<img id="xmTanImg_file4"/>
					
				</p>
			</td>
			<td>
				<p>
					<input type="file"  id="file5" οnchange="xmTanUploadImg(this)" accept="image/*"/><br>
					<img id="xmTanImg_file5"/>
				</p>
			</td>
			</tr>
			</table>
            
            
            
			
        </div>
        <hr />
        <script type="text/javascript">            
            //判断浏览器是否支持FileReader接口
            if (typeof FileReader == 'undefined') {
                document.getElementById("xmTanDiv").InnerHTML = "<h1>当前浏览器不支持FileReader接口</h1>";
                //使选择控件不可操作
                this.setAttribute("disabled", "disabled");
            }

            //选择图片,马上预览
            function xmTanUploadImg(obj) {
                var file = obj.files[0];
                console.log(obj);console.log(file);
                console.log("file.size = " + file.size);  //file.size 单位为byte

                var reader = new FileReader();

                //读取文件过程方法
                reader.onloadstart = function (e) {
                    console.log("开始读取....");
                }
                reader.onprogress = function (e) {
                    console.log("正在读取中....");
                }
                reader.onabort = function (e) {
                    console.log("中断读取....");
                }
                reader.onerror = function (e) {
                    console.log("读取异常....");
                }
                reader.onload = function (e) {
                    console.log("成功读取....");
                    var img = document.getElementById("xmTanImg_" + obj.id);
                    img.src = e.target.result;
					
                    //或者 img.src = this.result;  //e.target == this
                }

                reader.readAsDataURL(file)
            }
        </script>
</body>



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

andeyeluguo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值