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

JS 限制上传文件类型

<SCRIPT language="JavaScript">

<!-- Hide script from older browsers

?

extArray = new Array(".gif", ".jpg", ".png");

?

function LimitAttach(form, file) {

allowSubmit = false;

if (!file) return;

while (file.indexOf("\\") != -1)

file = file.slice(file.indexOf("\\") + 1);

ext = file.slice(file.indexOf(".")).toLowerCase();

for (var i = 0; i < extArray.length; i++) {

if (extArray[i] == ext) { allowSubmit = true; break; }

}

if (allowSubmit) form.submit();

else

alert("Please only upload files that end in types: ?"

+ (extArray.join(" ?")) + "\nPlease select a new "

+ "file to upload and submit again.");

}

// ?End -->

</script>

?

<body>

?

?

? ?Click "Browse" to choose the photo from your computer.<BR>

? ?Please upload only images that end in: ?

? ?<script> document.write(extArray.join(" ?"));

? ?</script><BR>

? ?Then click "Upload Your Photo" to upload it

?

?

<form method="POST" action="newphoto.asp" enctype="multipart/form-data" id=form1 name=form1>

<input type="file" name="UploadForm" size="30"><BR><BR>

<input type="button" name="submit" value="Upload Your Photo!" onclick="LimitAttach(this.form, this.form.UploadForm.value)">

?

———————————————————

<form onsubmit= "return a(this) ">

<input type=file name=f>

<input type=submit>

</form>

<script>

function a(o)

{

? s=o.f.value

? s=s.substr(s.lastIndexOf( ". ")+1)

? if(s!= "gif ")

? {

? alert("只能gif")

? return false

? }

}

</script>?

————————————————————

JS前端检测上传文件类型以及属性大小,并生成预览

?

<script language=javascript>

var ImgObj=new Image(); ? ? ?//建立一个图像对象

var AllImgExt=".jpg|.jpeg|.gif|.bmp|.png|"//全部图片格式类型

var FileObj,ImgFileSize,ImgWidth,ImgHeight,FileExt,ErrMsg,FileMsg,HasCheked,IsImg//全局变量 图片相关属性

//以下为限制变量

var AllowExt=".jpg|.gif|.doc|.txt|" //允许上传的文件类型 ?为无限制 每个扩展名后边要加一个"|" 小写字母表示

//var AllowExt=0

var AllowImgFileSize=70; ? ?//允许上传图片文件的大小 0为无限制 单位:KB

var AllowImgWidth=500; ? ? ?//允许上传的图片的宽度 ?为无限制 单位:px(像素)

var AllowImgHeight=500; ? ? ?//允许上传的图片的高度 ?为无限制 单位:px(像素)

HasChecked=false;

function CheckProperty(obj) ? ?//检测图像属性

{

FileObj=obj;

if(ErrMsg!="") ? ? ?//检测是否为正确的图像文件 返回出错信息并重置

{

? ? ShowMsg(ErrMsg,false);

? ? return false; ? ? ?//返回

}

?

if(ImgObj.readyState!="complete") //如果图像是未加载完成进行循环检测

{

? ? setTimeout("CheckProperty(FileObj)",500);

? ? return false;

}

?

ImgFileSize=Math.round(ImgObj.fileSize/1024*100)/100;//取得图片文件的大小

ImgWidth=ImgObj.width ? ? ?//取得图片的宽度

ImgHeight=ImgObj.height; ? ?//取得图片的高度

FileMsg="\n图片大小:"+ImgWidth+"*"+ImgHeight+"px";

FileMsg=FileMsg+"\n图片文件大小:"+ImgFileSize+"Kb";

FileMsg=FileMsg+"\n图片文件扩展名:"+FileExt;

?

if(AllowImgWidth!=0&&AllowImgWidth<ImgWidth)

? ? ErrMsg=ErrMsg+"\n图片宽度超过限制。请上传宽度小于"+AllowImgWidth+"px的文件,当前图片宽度为"+ImgWidth+"px";

?

if(AllowImgHeight!=0&&AllowImgHeight<ImgHeight)

? ? ErrMsg=ErrMsg+"\n图片高度超过限制。请上传高度小于"+AllowImgHeight+"px的文件,当前图片高度为"+ImgHeight+"px";

?

if(AllowImgFileSize!=0&&AllowImgFileSize<ImgFileSize)

? ? ErrMsg=ErrMsg+"\n图片文件大小超过限制。请上传小于"+AllowImgFileSize+"KB的文件,当前文件大小为"+ImgFileSize+"KB";

?

if(ErrMsg!="")

? ? ShowMsg(ErrMsg,false);

else

? ? ShowMsg(FileMsg,true);

}

?

ImgObj.onerror=function(){ErrMsg='\n图片格式不正确或者图片已损坏!'}

?

function Sho