JavaScript上传图片前判断格式并预览(按比例)
|
admin
2010年3月8日 0:40
本文热度 11253
|
javascript判断上传图片的格式,并按比例预览图片,比较实用的一个javascript应用,在ie8下测试也兼容,代码简洁,复制到你的网页就可以用了。
<title>
上传图片前判断格式并预览
</title>
<script>
function yulan()
{
var fileext=document.form1.UpFile.value.substring(document.form1.UpFile.value.lastIndexOf("."),document.form1.UpFile.value.length)
fileext=fileext.toLowerCase()
if ((fileext!='.jpg')&&(fileext!='.gif')&&(fileext!='.jpeg')&&(fileext!='.png')&&(fileext!='.bmp'))
{
alert(" www.codefans.net 友情提示:请上传规定格式的图片,谢谢 !");
document.form1.UpFile.focus();
}
else
{
//alert(''+document.form1.UpFile.value)//把这里改成预览图片的语句
document.getElementById("preview").innerHTML="<img src='"+document.form1.UpFile.value+"' width=120 style='border:6px double #ccc'>"
}
}
</script>
<input type="file" name="UpFile" size="50" onchange="yulan()"/>
<div id="preview"></div>
该文章在 2026/4/13 19:02:02 编辑过