asp类编写的上传代码

可以直接复制可以测试哦!

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
Response.Buffer=True
Server.ScriptTimeOut=9999999

On Error Resume Next
%>

<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
</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("&nbsp;&nbsp;&nbsp;<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("&nbsp;&nbsp;&nbsp;<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>

上面的一部分是类文件.你也可以把他分开,在进行包含就可以了


本文来自:PHP小戴(www.phpxd.com) 详细出处参考:http://www.phpxd.com/yuandaima/2008-12-02/200812021009070.html

无组件ASP文件上传代码 记得在建立一个文件夹"updata" saveannounce_upload.asp 上传页 ------------------------------------ body {font-size:9pt;} input {font-size:9pt;} 文件上传 文件 ------------------------------------ saveannouce_upfile.asp 保存文件到服务器 ------------------------------------ 文件上传 <% dim upload,file,formName,formPath set upload=new upload_5xSoft ''建立上传对象 formPath=upload.form("filepath") ''在目录后加(/) if right(formPath,1)"/" then formPath=formPath&"/" for each formName in upload.file ''列出所有上传了的文件 set file=upload.file(formName) ''生成一个文件对象 if file.filesize<100 then response.write "请先选择你要上传文件 [ 重新上传 ]" response.end end if if file.filesize>500*1000 then '设置上传文件大小为500K response.write "文件大小超过了限制 500K [ 重新上传 ]" response.end end if if file.FileSize>0 then ''如果 FileSize > 0 说明有文件数据 file.SaveAs Server.mappath("updata\"&file.FileName) ''保存文件 end if set file=nothing next set upload=nothing response.write "文件上传成功 [ 继续上传 ]" %> ------------------------------------ upload.inc 建立upload对象 ------------------------------------ dim upfile_5xSoft_Stream Class upload_5xSoft dim Form,File,Version Private Sub Class_Initialize dim iStart,iFileNameStart,iFileNameEnd,iEnd,vbEnter,iFormStart,iFormEnd,theFile dim strDiv,mFormName,mFormValue,mFileName,mFileSize,mFilePath,iDivLen,mStr Version="" if Request.TotalBytes<1 then Exit Sub set Form=CreateObject("Scripting.Dictionary") set File=CreateObject("Scripting.Dictionary") set upfile_5xSoft_Stream=CreateObject("Adodb.Stream") upfile_5xSoft_Stream.mode=3 upfile_5xSoft_Stream.type=1 upfile_5xSoft_Stream.open upfile_5xSoft_Stream.write Request.BinaryRead(Request.TotalBytes) vbEnter=Chr(13)&Chr(10) iDivLen=inString(1,vbEnter)+1 strDiv=subString(1,iDivLen) iFormStart=iDivLen iFormEnd=inString(iformStart,strDiv)-1 while iFormStart 0 and iFileNameStartiStart then mFileSize=iEnd-iStart-4 else mFileSize=0 end if set theFile=new FileInfo theFile.FileName=getFileName(mFileName) theFile.FilePath=getFilePath(mFileName) theFile.FileSize=mFileSize theFile.FileStart=iStart+4 theFile.FormName=FormName file.add mFormName,theFile else iStart=inString(iEnd+1,vbEnter&vbEnter) iEnd=inString(iStart+4,vbEnter&strDiv) if iEnd>iStart then mFormValue=subString(iStart+4,iEnd-iStart-4) else mFormValue="" end if form.Add mFormName,mFormValue end if iFormStart=iformEnd+iDivLen iFormEnd=inString(iformStart,strDiv)-1 wend End Sub Private Function subString(theStart,theLen) dim i,c,stemp upfile_5xSoft_Stream.Position=theStart-1 stemp="" for i=1 to theLen if upfile_5xSoft_Stream.EOS then Exit for c=ascB(upfile_5xSoft_Stream.Read(1)) If c > 127 Then if upfile_5xSoft_Stream.EOS then Exit for stemp=stemp&Chr(AscW(ChrB(AscB(upfile_5xSoft_Stream.Read(1)))&ChrB(c))) i=i+1 else stemp=stemp&Chr(c) End If Next subString=stemp End function Private Function inString(theStart,varStr) dim i,j,bt,theLen,str InString=0 Str=toByte(varStr) theLen=LenB(Str) for i=theStart to upfile_5xSoft_Stream.Size-theLen if i>upfile_5xSoft_Stream.size then exit Function upfile_5xSoft_Stream.Position=i-1 if AscB(upfile_5xSoft_Stream.Read(1))=AscB(midB(Str,1)) then InString=i for j=2 to theLen if upfile_5xSoft_Stream.EOS then inString=0 Exit for end if if AscB(upfile_5xSoft_Stream.Read(1))AscB(MidB(Str,j,1)) then InString=0 Exit For end if next if InString0 then Exit Function end if next End Function Private Sub Class_Terminate form.RemoveAll file.RemoveAll set form=nothing set file=nothing upfile_5xSoft_Stream.close set upfile_5xSoft_Stream=nothing End Sub Private function GetFilePath(FullPath) If FullPath "" Then GetFilePath = left(FullPath,InStrRev(FullPath, "\")) Else GetFilePath = "" End If End function Private function GetFileName(FullPath) If FullPath "" Then GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1) Else GetFileName = "" End If End function Private function toByte(Str) dim i,iCode,c,iLow,iHigh toByte="" For i=1 To Len(Str) c=mid(Str,i,1) iCode =Asc(c) If iCode255 Then iLow = Left(Hex(Asc(c)),2) iHigh =Right(Hex(Asc(c)),2) toByte = toByte & chrB("&H"&iLow) & chrB("&H"&iHigh) Else toByte = toByte & chrB(AscB(c)) End If Next End function End Class Class FileInfo dim FormName,FileName,FilePath,FileSize,FileStart Private Sub Class_Initialize FileName = "" FilePath = "" FileSize = 0 FileStart= 0 FormName = "" End Sub Public function SaveAs(FullPath) dim dr,ErrorChar,i SaveAs=1 if trim(fullpath)="" or FileSize=0 or FileStart=0 or FileName="" then exit function if FileStart=0 or right(fullpath,1)="/" then exit function set dr=CreateObject("Adodb.Stream") dr.Mode=3 dr.Type=1 dr.Open upfile_5xSoft_Stream.position=FileStart-1 upfile_5xSoft_Stream.copyto dr,FileSize dr.SaveToFile FullPath,2 dr.Close set dr=nothing SaveAs=0 end function End Class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值