<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
Response.Buffer=True
Server.ScriptTimeOut=9999999 '一千萬
On Error Resume Next
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
<!--
= * = = * = = * = = * = = * = = * = = * = = * = = * = = * = = * = = *
* 作 者: 我本有心
= QQ: 381584252
* E-Mail: hztgcl1986@163.com
= 轉載請註明出處及作者!
* 版權所有,侵權必究!!!
=
* http://www.8848so.com,人物搜索,8848So
= * = = * = = * = = * = = * = = * = = * = = * = = * = = * = = * = = *
-->
</head>
<body>
<%
Class HZTUpload
Public filesize,filetype,filepath,reservefilename,formid,txtid
Private formsize,formdata,bincrlf,oencrlfplace,twocrlfplace,ext,p,l,filename,savefilepath,rndfilename
Private usingstream,stream,fso
Private Sub Class_Initialize
filesize=1024 '文件大小,k
filetype="gif,png,jpg,jpeg" '文件類型
filepath="Upload" '保存目錄
reservefilename=0 '0:不保留原文件名,1:保留原文件名
formid="myform"
txtid="txt"
Randomize()
'系統生成文件名
rndfilename=Year(Now())&Month(Now())&Day(Now())&Hour(Now())&Minute(Now())&Second(Now())&Int((999999-100000+1)*Rnd()+100000)
Set usingstream=Server.CreateObject("ADODB.Stream")
Set stream=Server.Createobject("ADODB.Stream")
Set fso=Server.CreateObject("Scripting.FileSystemObject")
End Sub
Private Sub Class_Terminate
usingstream.Close():Set usingstream=Nothing
stream.Close():Set stream=Nothing
Set fso=Nothing
End Sub
Sub Upload() '要返回的form和text的id
If Right(filepath,1)<>"/" Then filepath=filepath&"/"
formsize=Request.TotalBytes
formdata=Request.BinaryRead(formsize)
usingstream.Type=1
usingstream.Open()
usingstream.Write(formdata)
bincrlf=ChrB(13)&ChrB(10) '二進制回車換行
oencrlfplace=InStrB(formdata,bincrlf) '44,第一次回車換行位置
twocrlfplace=InStrB(oencrlfplace+1,formdata,bincrlf) '第二次回車換行位置
stream.Type=1
stream.Open()
usingstream.Position=oencrlfplace+1
usingstream.CopyTo stream,twocrlfplace-oencrlfplace-3 '得到第二行數據,twocrlfplace-onecrlfplace-("長度)
stream.Position=0
stream.Type=2 '字符串
stream.CharSet="GB2312"
streamtext=stream.Readtext() '讀取第二行數據
stream.Close()
filename=Mid(streamtext,InstrRev(streamtext,"")+1) '得到文件名
p=InStrB(formdata,bincrlf&bincrlf)+4 '4爲兩次回車換行長度
l=InStrB(p+1,formdata,LeftB(formdata,oencrlfplace-1))-p-2 '文件內容部分長度,onecrlfplace-1爲第一行數據(也是分隔符),2爲回車換行長度
stream.Type=1
stream.Open()
usingstream.Position=p-1
usingstream.CopyTo stream,l '文件內容數據
'---------------------------------------------------------------------------------------------------
Call CheckFolder(filepath) '檢測文件夾是否存在,如果不存在則創建
ext=Right(filename,1+Len(filename)-InStrRev(filename,".")) '文件擴展名:.gif
If reservefilename=0 Then '自動命名
savefilepath=Server.MapPath(filepath&rndfilename&ext)
filename=rndfilename&ext
Else '保留原文件名
filename=CheckFile(Left(filename,InStrRev(filename,".")-1),ext)
savefilepath=Server.MapPath(filepath&filename)
End If
If CheckExt(Mid(ext,2))=False Then Call Message(1) '檢測文件類型
If ceil(stream.Size/1024)>filesize Then Call Message(2)'檢測文件大小
'---------------------------------------------------------------------------------------------------
stream.SaveToFile savefilepath,2 '保存文件
If Err.Number=0 Then
Call Message(0)
Else
Call Message(404)
End If
End Sub
Function ceil(v) '實現JS中Math.ceil()
If v>0 Then
v=Fix(v)+Sgn(v-Fix(v))
Else
v=Fix(v)
End If
ceil=v
End Function
Function CheckFolder(foldername) '檢測文件夾是否存在,如果不存在則創建
If fso.FolderExists(Server.MapPath(foldername)) Then
Exit Function
Else
fso.CreateFolder(Server.MapPath(foldername))
End If
End Function
Function CheckFile(fname,ext) '檢測文件是否存在,如果存在則重命名,如:重名文件(1).txt
If fso.FileExists(Server.MapPath(filepath&fname&ext)) Then
Dim i
i=1
Do While (fso.FileExists(Server.MapPath(filepath&fname&"("&i&")"&ext)))
i=i+1
Loop
CheckFile=fname&"("&i&")"&ext
Else
CheckFile=fname&ext
End If
End Function
Function CheckExt(ext) '檢測文件類型合法性
Dim i,istrue,exts
exts=Split(filetype,",")
For i=0 To UBound(exts)
If LCase(ext)=exts(i) Then
istrue=True
Exit For
Else
istrue=False
End If
Next
CheckExt=istrue
End Function
Sub Message(mi)
Select Case mi
Case 1:
Response.Write("<script>")
Response.Write("window.alert('文件類型非法!');history.back();")
Response.Write("</script>")
Response.End()
Case 2:
Response.Write("<script>")
Response.Write("window.alert('文件大小超過限制!');history.back();")
Response.Write("</script>")
Response.End()
Case 0:
Response.Write("<font color='0000FF'>文件上傳成功!</font>")
Response.Write(" <a href='"&Request.ServerVariables("URL")&"'>重新上傳</a>")
Response.Write("<script>")
Response.Write("window.top.document."&formid&"."&txtid&".value='"&filename&"';")
Response.Write("</script>")
Response.End()
Case 404:
Response.Write("<font color='FF0000'>文件上傳失敗!</font>")
Response.Write(" <a href='"&Request.ServerVariables("URL")&"'>重新上傳</a>")
Response.End()
End Select
End Sub
End Class
If Request.TotalBytes>0 Then
Set hg=New HZTUpload
'hg.filepath="Pic/" '文件保存路徑,默認:Upload
'hg.filetype="gif,png,jpg,jpeg,rar" '文件類型,默認:gif,png,jpg,jpeg
'hg.filesize=1024 '文件大小,單位k,默認:1024
'hg.reservefilename=0 '是否保留原文件名,0:否,1:是,默認:0
hg.formid="mf" '接收文件名的form的id,默認:myform
hg.txtid="txt" '接收文件名的text的id,默認:txt
hg.Upload() '保存文件,form名稱,text名稱
Else
%>
<form id="mf" name="mf" method="post" action="<%=Request.ServerVariables("URL")%>" enctype="multipart/form-data">
<input type="file" id="f" name="f" />
<br />
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</form>
<%End If%>
</body>
</html>