检测图片大小以及设置图片路径

Others 2017-11-07 12:54:20 2017-11-07 12:54:20 2398 次浏览
function fileChange(target,id) { 
var fileSize = 0; 
var filetypes =[".jpg",".png",".jpeg",".gif"]; 
var filepath = target.value; 
var filemaxsize = 1024*2;//2M 
if(filepath){ 
var isnext = false; 
var fileend = filepath.substring(filepath.indexOf(".")); 
if(filetypes && filetypes.length>0){ 
for(var i =0; i<filetypes.length;i++){ 
if(filetypes[i]==fileend){ 
isnext = true; 
break; 
} 
} 
} 
if(!isnext){ 
alert("不接受此文件类型!"); 
target.value =""; 
return false; 
} 
}else{ 
return false; 
} 
if (isIE && !target.files) { 
var filePath = target.value; 
var fileSystem = new ActiveXObject("Scripting.FileSystemObject"); 
if(!fileSystem.FileExists(filePath)){ 
	$("#car_img img").attr("src","../page/images/addimage.png");
alert("附件不存在,请重新输入!"); 
return false; 
} 
var file = fileSystem.GetFile (filePath); 
fileSize = file.Size; 
} else { 
fileSize = target.files[0].size; 
} 

var size = fileSize / 1024; 
if(size>filemaxsize){ 
	$("#news_img").attr("src","../page/images/addimage.png");
alert("附件大小不能大于"+filemaxsize/1024+"M!"); 
target.value =""; 
return false; 
} 
if(size<=0){ 
	$("#news_img").attr("src","../page/images/addimage.png");
alert("附件大小不能为0M!"); 
target.value =""; 
return false; 
} 
return true;
} 
 var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
function imgchange(obj){
	if(fileChange(obj)){
		if(navigator.userAgent.indexOf("MSIE")>-1){
	$("#news_img").attr("src",obj.value);
	}else{
		$("#news_img").attr("src",URL.createObjectURL(obj.files[0]));
	}
	}
	


	
	
}