if errMsg="" then '----如果到此还没有错误 For Each strFormElement In oFileUp.Form If IsObject(oFileUp.Form(strFormElement)) Then '如果是文件 If Not oFileUp.Form(strFormElement).IsEmpty Then '--文件不为空 flagOK=1 '--不是文件或文件大于限制,设置错误信息 If oFileUp.Form(strFormElement).TotalBytes<100 Then flagOK=0 ElseIf oFileUp.Form(strFormElement).TotalBytes> upFileSize Then flagOK=0 errMsg=errMsg+"文件:" errMsg = errMsg & oFileUp.Form(strFormElement).UserFileName errMsg = errMsg & " 大于"&upFileSize\1024&"KB!<br>" Else If strRestrictBy = "extension" Then '--验证方式为扩展名 strShortFileName = mid(oFileUp.Form(strFormElement).UserFileName,InStrRev(oFileUp.Form(strFormElement).UserFileName, "\")+1) '取得文件名 strExtension = Mid(strShortFileName, InStrRev(strShortFileName, ".")) '取得扩展名 '======检查后缀名==== Select Case LCase(strExtension) Case ".jpg", ".gif", ".bmp",".png"
Case Else flagOK=0 oFileUp.Form(strFormElement).Delete Response.Write("<B>错误:</B> 扩展名为 <I>") Response.Write(strExtension) Response.Write ( "</I> 的文件不能被上传。<BR>") End Select Else '--验证方式为MIME类型 strContentType = oFileUp.Form(strFormElement).ContentType Select Case LCase(strContentType) Case "image/gif", "image/jpeg", "image/pjpeg" Case Else flagOK=0 oFileUp.Form(strFormElement).Delete Response.Write("<B>错误:</B> MIME类型为 <I>") Response.Write (strContentType) Response.Write ("</I> 的文件不能被上传。<BR>") End Select End If '--end if 验证方式 End If 'end if 文件大小判断
If flagOK=1 Then '如果文件通过检查,保存文件,并插入数据库纪录 randomize ranNum=int(900*rnd)+100 filename=year(now())&month(now())&day(now()) filename = filename &hour(now())&minute(now()) filename = filename&second(now())&ranNum filename = filename&LCase(strExtension) oFileUp.Form(strFormElement).SaveInVirtual formPath&filename '让文件名不重复,保存文件,这里用的是SaveInVirtual方法
'--输出该文件的大小 Response.Write "<B>大小:</B>"& Response.Write oFileUp.Form(strFormElement).TotalBytes Response.Write "<BR>" '===添加文件的信息到数据库里=== myIndex=right(strFormElement,1) '--取得文件的序号,如file1则取得为1,file2取得为2 temp_photoTitle=oFileUp.form("photoTitle"+myIndex) '--这四行取得对应的标题,简介,宽度,高度 temp_photoIntro=oFileUp.form("photoIntro"+myIndex) temp_photoWidth=oFileUp.form("photoWidth"+myIndex) temp_photoHeight=oFileUp.form("photoHeight"+myIndex) '====检查输入,为空则给初值== temp_photoTitle=replace(trim(temp_photoTitle),"'","''") if temp_photoTitle="" then temp_photoTitle="没有填写" end if temp_photoIntro=replace(trim(temp_photoIntro),"'","''") if temp_photoIntro="" then temp_photoIntro="没有填写" end if if temp_photoWidth="" or not IsNumeric(temp_photoWidth) then temp_photoWidth=160 end if if temp_photoHeight="" or not IsNumeric(temp_photoHeight) then temp_photoHeight=120 end if '===插入数据库=== FileSize=oFileUp.Form(strFormElement).TotalBytes sql = "insert into TBL_PHOTO(albumID, groupID, userName, addTime, photoFilename, photoTitle, photoIntro, photoClick, photoSize, photoWidth, photoHeight, locked,viewPassword) values(" & albumID & "," & groupID & ",'" & session("userName") & "','" & Now() & "','" & filename & "','" & temp_photoTitle & "','" & temp_photoIntro & "',1," & FileSize & "," & temp_photoWidth & "," & temp_photoHeight & ",'no','')" conn.execute sql sql="update TBL_ALBUM set photoCount=photoCount+1 where albumID="&albumID conn.execute sql sql="update TBL_GROUP set photoCount=photoCount+1 where groupID="&groupID conn.execute sql '===输出上传成功信息=== iCount=iCount+1 End If
Else Response.Write strFormElement & "对象为空!" End If '--end if 对象为空 End If '--end if 是否是文件 Next Set oFileUp = Nothing '删除此对象 end if '--end if 没有错误信息
response.write "<br>"&iCount&" 个文件上传结束!" response.write "<br><a href='photo_listphoto.asp?albumID="&albumID&"'><B>返回相册</B></a>" '=====如果有错,输出错误信息===== if errMsg<>"" then response.write "<br>"&errMsg response.write "<INPUT type='button' onClick='history.go(-1)' value='返回' class='myInput'>" end if conn.close set conn=nothing %>