天天看點

上傳照片并顯示

上傳一張照片并顯示

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