通过客户端验证上传图片文件大小的ASP源码

2018-09-06 12:25

阅读:380

  <%@Language=JScript@CodePage=936%>
<ScriptLanguage=JScriptRunAt=Server>
/****************************************************************\
<lostinet:sourcexmlns:lostinet=lostinet-d2g-com/source>
<lostinet:source-info>
<lostinet:name>Lostinet_ASP_Upload的例子-动态表单部分</lostinet:name>
<lostinet:description>一个动态的表单的例子</lostinet:description>
</lostinet:source-info>
<lostinet:author-info>
<lostinet:name>Lostinet</lostinet:name>
<lostinet:homepage>
</lostinet:author-info>
<lostinet:copyright-info>
<lostinet:copyright>版权声明:这个软件可以随意发布。也可以根据具体情况进行优化修改。但是请保留作者的相关信息。</lostinet:copyright>
</lostinet:copyright-info>
</lostinet:source>
\****************************************************************/
</Script>
<SCRIPTRUNAT=ServerLANGUAGE=JScript>
</SCRIPT>
<HTML>
<HEAD>
<TITLE>JScriptExampleForm</TITLE>
<METAHTTP-EQUIV=Content-TypeCONTENT=text/html;charset=gb2312/>
</HEAD>
<STYLE>
button
{
border:1pxgraysolid;
}
div.form_item
{
width:500px;
overflow-x:visible;
padding:4px;
margin-top:8px;
border:2pxgraydotted;
}
input.file
{
border:1pxgrayinset;
width:400px;
}
{
border:1pxgrayinset;
width:400px;
SCROLLBAR-FACE-COLOR:#cccccc;
SCROLLBAR-HIGHLIGHT-COLOR:WHITE;
SCROLLBAR-SHADOW-COLOR:WHITE;
SCROLLBAR-ARROW-COLOR:maroon;
SCROLLBAR-TRACK-COLOR:#e0e4e4;
SCROLLBAR-3DLIGHT-COLOR:#cccccc;
SCROLLBAR-DARKSHADOW-COLOR:#cccccc;
SCROLLBAR-BASE-COLOR:#cccccc;
}
</STYLE>
<SCRIPT>
varcountImgLoading=0;
functionGetItemDiv(e)
{
while(e=e.parentElement)
{
if(e.className.toLowerCase()==form_item)
{
returne;
}
}
throw(newError(-1,Impossible:GetItemDiv发生不能处理的意外));
}
functionGetNamedItem(div,name)
{
returndiv.all(name);
throw(newError(-11,Impossible:GetNamedItem发生不能处理的意外+name));
}
functionCheck_Item(div)
{
varfile=GetNamedItem(div,file);
varinfo=GetNamedItem(div,info);
varwidth=GetNamedItem(div,width);
varheight=GetNamedItem(div,height);
varpreview=GetNamedItem(div,preview);
if(file.value.replace(/\s/g,)==)
{
file.focus();
returnfalse;
}
if(info.value.replace(/\s/g,)==)
{
info.focus();
returnfalse;
}
if(info.value.length>1000)
{
alert(描述的长度不能超过1000字);
info.focus();
returnfalse;
}
if(height.value==0width.value==0)
{
file.focus();
returnfalse;
}
if(parseInt(height.value)>800parseInt(width.value)>600)
{
alert(图片尺寸大于800X600);
file.focus();
returnfalse;
}
{
alert(只能上传本地文件!!\n请把文件框的内容清楚\n然后按浏览);
file.focus();
returnfalse;
}
if(preview.fileSize==0)
{
file.focus();
returnfalse;
}
returnpreview.fileSize;
}
functionCheck_Form()
{
if(event==null)varevent=newObject();
vartotalSize=0;
varhaveItem=false;
varcoll=form_submit.children;
for(vari=0;i<coll.length;i++)
{
varitem=coll.item(i);
if(item.className.toLowerCase()==form_item)
{
haveItem=true;
totalSize=Check_Item(item);
if(totalSize==false)
{
alert(该输入框必须填上正确的内容);
returnevent.returnValue=false;
}
}
}
if(haveItem==false)
{
Add_FormItem();
returnevent.returnValue=false;
}
if(totalSize==0)
{
alert(不打算上传了);//暂时没有可能执行
returnevent.returnValue=false;
}
if(totalSize>1024*1024)
{
alert(不能上传超过1M的数据);
returnevent.returnValue=false;
}
btn_submit.disabled=true;
returnevent.returnValue=true;
}
functionAdd_FormItem()
{
form_submit.elements(input_submit).insertAdjacentHTML(beforebegin,form_template.innerHTML);
}
functionDelete_FormItem()
{
varbtn=event.srcElement;
GetItemDiv(btn).removeNode(true);
}
functionShow_Preview()
{
vardiv=GetItemDiv(event.srcElement);
varpreview=GetNamedItem(div,preview);
varfile=GetNamedItem(div,file);
if(preview.readyState==uninitializedpreview.readyState==complate)countImgLoading++;
btn_submit.disabled=true;
preview.src=file.value;
}
functionOnPreviewLoad(secceed)
{
btn_submit.disabled=false;//bug:当表单在提交的时候,忽略
countImgLoading--;
vardiv=GetItemDiv(event.srcElement);
varpreview=GetNamedItem(div,preview);
varheight=GetNamedItem(div,height);
varwidth=GetNamedItem(div,width);
if(secceed)
{
height.value=preview.height;
width.value=preview.width;
}
else
{
height.value=width.value=0;
}
varfilesize=GetNamedItem(div,filesize);
filesize.innerText=Math.floor(preview.fileSize/1024)+K;
}
functionOnPreviewMouseOver(div)
{
if(div.contains(event.fromElement))return;
vardivItem=GetItemDiv(div);
varpreview=GetNamedItem(divItem,preview);
div.style.overflow=visible;
preview.style.position=absolute;
}
functionOnPreviewMouseOut(div)
{
if(div.contains(event.toElement))return;
vardivItem=GetItemDiv(div);
varpreview=GetNamedItem(divItem,preview);
div.style.overflow=hidden;
preview.style.position=static;
}
functionOnPreviewClick(div)
{
vardivItem=GetItemDiv(event.srcElement);
varpreview=GetNamedItem(divItem,preview);
div.style.overflow=hidden;
preview.style.position=static;
}
</SCRIPT>
<BODY>
<DIV>
<BUTTONID=btn_add>添加一个需要上传的图片</BUTTON>
<BUTTONID=btn_submit>提交所有内容</BUTTON>
</DIV>
<DIV>
<FORMID=form_submitACTION=e.g.jscript.action.aspMETHOD=POSTENCTYPE=multipart/form-dataONSUBMIT=Check_From()>
<INPUTID=input_submitTYPE=submitSTYLE=display:none>
</FORM>
</DIV>

<FORMID=form_templateSTYLE=display:none;>
<DIVCLASS=form_item>
<DIV>
要上传图片(<SPANid=filesize></SPAN>)的路径:<BUTTONCLASS=deleteONCLICK=Delete_FormItem()>删除</BUTTON>
</DIV>
<DIVSTYLE=border:1pxorangesolid;width:200px;height:50px;overflow:hidden;ONMOUSEOVER=OnPreviewMouseOver(this)ONMOUSEOUT=OnPreviewMouseOut(this)ONCLICK=OnPreviewClick(this)>
<INPUTTYPE=hiddenNAME=widthVALUE=0>
<INPUTTYPE=hiddenNAME=heightVALUE=0>
<IMGID=previewSTYLE=position:staticONLOAD=OnPreviewLoad(true)ONERROR=OnPreviewLoad(false)>
</DIV>
<DIV>
<INPUTTYPE=fileNAME=fileCLASS=fileONCHANGE=Show_Preview()>
</DIV>
<DIV>
相关的描述:
</DIV>
<DIV>
<TEXTAREANAME=infoCLASS=infoROWS=4></TEXTAREA>
</DIV>
</DIV>
</FORM>
</BODY>
<SCRIPT>
Add_FormItem();
functionbtn_add.onclick()
{
Add_FormItem();
}
functionbtn_submit.onclick()
{
if(Check_Form())
form_submit.submit();
}
</SCRIPT>
</HTML>


评论


亲,登录后才可以留言!