ASP文件上传类




< %@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(
" &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 >
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值