实现在上传图片的同时预览已选择的图片效果............................
代码如下:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title><script language="JavaScript" type="text/JavaScript">
<!--
var flag=false;
function DrawImage(ImgD){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= 120/80){
if(image.width>120){
ImgD.width=120;
ImgD.height=(image.height*120)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>80){
ImgD.height=80;
ImgD.width=(image.width*80)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
}
}
//-->
function changesrc(){
document.yourpic.src = document.getElementsByName("filename")[0].value;
DrawImage(yourpic);
var img = new Image();
img.src = document.yourpic.src;
alert("fileSize = "+ img.fileSize/1024 +" KB");
}
</script>
</head>
<body>
<table width="100%" border="0">
<tr>
<td>
<div align="center"><img border="1" name="yourpic" οnlοad="javascript:DrawImage(this);">
</div>
</td>
</tr>
<form name="picForm" action="FileUploadServlet" method="post" enctype="multipart/form-data" οnsubmit="return checkPic(picForm);">
<tr>
<td>
<div align="center"><input type="file" name="filename" οnchange="changesrc()"></div>
</td>
</tr>
<tr>
<td>
<div align="center"><input type="submit" value="上传照片" /></div>
</td>
</tr>
</form>
</table>
</body>
贴到一个hmt页面里就可以看到效果