js:实现上传图片即时预览

图片上传即时预览是很多地方都用的上的 抽空封装了一段即时预览代码

 	function setImagePreview(avalue) {
        	//获取预览按钮
        	var docObj=document.getElementById("doc");
       	 	//获取预览图片容器
        	var imgObjPreview=document.getElementById("preview");
        	//Firefox
        	if(docObj.files &&docObj.files[0]){
            	imgObjPreview.style.width = '224px';
            	imgObjPreview.style.height = '252px'; 
     
            	imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]);
        	}else{
            	//IE
            	docObj.select();
            	var imgSrc = document.selection.createRange().text;
            	var localImagId = document.getElementById("localImag");
            	localImagId.style.width = "224px";
            	localImagId.style.height = "252px";
            	try{
                	localImagId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
                	ocalImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
            	}catch(e){
                	alert("您上传的图片格式不正确,请重新选择!");
                	return false;
            	}
            	imgObjPreview.style.display = 'none';
            	document.selection.empty();
        	}
            	return true;
    	}





你可能感兴趣的:(JavaScript)