JS-input上传图片预览

image

代码

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
<html>  
<head>
<script type="text/javascript" src="jquery.min.js"></script>
<script>
$(function(){
$("# pstimg").change(function(){
var file = this.files[0];
alert("文件大小:"+(file.size / 1024).toFixed(1)+"kB");
if (window.FileReader) {
var reader = new FileReader();
reader.readAsDataURL(file);
//监听文件读取结束后事件
reader.onloadend = function (e) {
showXY(e.target.result,file.fileName);
};
}
});
});
function showXY(source){
var img = document.getElementById("loc_img");
img.src = source;
alert("Width:"+img.width+", Height:"+img.height);
}
</script>
</head>
<body>
<input type="file" name="pstimg" id="pstimg"/>
<img src="" id="loc_img" />
<body>
</html>
-------------本文结束感谢您的阅读-------------
0%