日期:2014-05-16  浏览次数:20365 次

IE8 上传图片预览的JS(转)

function photoPreView(image) {
var photoPreView = document.getElementById( "photoPreView" ) ;
if( image.value != '' ){
var checkimg = image.value.toLowerCase() ;
if ( !checkimg.match( /(\.jpg|\.JPG)$/ ) )
{
alert( "只能上传jpg格式的图片" ) ;
return false ;
}else{
var path = getPath( image ) ;
photoPreView.style.width = "100px";
photoPreView.style.height = "100px";
photoPreView.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod = scale)";
photoPreView.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = path;
}
}
}

function getPath(obj)
{
if(obj)
{
if ( window.navigator.userAgent.indexOf("MSIE") >= 1 )
{
obj.select() ;
return document.selection.createRange().text ;
} else if( window.navigator.userAgent.indexOf("Firefox") >= 1 ){
if(obj.files)
{
return obj.files.item(0).getAsDataURL() ;
}
return obj.value ;
}
return obj.value ;
}
}

?

?

<input type="file" name = "photopath" onChange="photoPreView(this);" />
<div id = "photoPreView" ></div>