Javascript实现图片上传

html代码:

View Code
 <img id="comImg" class="pp" runat="server" alt="预览" onclick="ShowUp2();" width="100"
height
="100" style="display: none;" />
<div id="comImg_fake" class="pp" style="filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);
cursor: pointer; background: url(images/waitpic.gif); width: 100px; height: 100px;"

onclick
="ShowUp();">
</div>

             

Javascript代码:

View Code
<script type="text/javascript">
function pSelect() {//弹出选择窗口
window.open('selectTest.aspx', 'select', 'height=500, width=500, top=200, left=200, toolbar=no,menubar=no, scrollbars=no, resizable=no, status=no,location=no');
}
function ShowUp() {

// var h = document.getElementById("form1").style.height;
document.getElementById('upmain').style.display = 'block';
document.getElementById(
'upload').style.display = 'block';
document.getElementById(
"upload").style.left = (document.body.clientWidth - 250) / 2 + "px";
// document.getElementById("upload").style.top = (parseInt(h.substr(0, h.IndexOf('px'))) - 80) / 2 + "px";
//alert(document.getElementById("upload").style.top);
}
function ShowUp2() {
document.getElementById(
'comImg_fake').style.display = 'none';
document.getElementById(
'comImg').style.display = 'block';

ShowUp();
}
function CloseUp() {
document.getElementById(
'upmain').style.display = 'none';
document.getElementById(
'upload').style.display = 'none';
}
function GetPreview() {
if (CheckFormat()) {
document.getElementById(
'comImg').style.display = 'none';
document.getElementById(
"comImg_fake").style.background = "none";
try {
document.getElementById(
"comImg_fake").filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = document.getElementById("selectedfile").value;
CloseUp();
}
catch (e) { alert("文件损坏或非法!"); alert(e); }

}
else
alert(
"目前只支持格式为:\"jpg\"、\"bmp\"、\"tiff\"、\"gif\"、\"tga\"、\"jpeg\"、\"png\"的图片文件!");
}
function CheckFormat() {
var obj = document.getElementById("selectedfile").value;
var obj = obj.substr(obj.lastIndexOf('.') + 1).toLowerCase();
return (obj == "jpg" || obj == "bmp" || obj == "gif" || obj == "jpeg" || obj == "png");
}
function ShowImg() {

document.getElementById(
"comImg").style.display = "block";
document.getElementById(
"comImg_fake").style.display = "none";
}
</script>


作者:记忆逝去的青春
出处:http://www.cnblogs.com/lukun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过http://www.cnblogs.com/lukun/  联系我,非常感谢。

posted on 2011-04-08 15:22  记忆逝去的青春  阅读(442)  评论(1编辑  收藏  举报