asp无组件上传文件(分块--测试200M上传成功)

    技术2022-07-04  115

      接上一篇用aspupload组件上传文章,在这之前,尝试使用无组件上传,不过大于500M的就会报 “存储空间不足的错误”,对于想完成小文件上传的同志们,可以使用。

       初始设置不再赘述。直接看代码

     

        1. upload.inc

     <script RUNAT=SERVER LANGUAGE=VBscript>dim Data_5xsoft

    Class upload_5xsoft  dim objForm,objFile,Version

    Public function Form(strForm)   strForm=lcase(strForm)   if not objForm.exists(strForm) then     Form=""   else     Form=objForm(strForm)   end ifend function

    Public function File(strFile)   strFile=lcase(strFile)   if not objFile.exists(strFile) then     set File=new FileInfo   else     set File=objFile(strFile)   end ifend function

    Private Sub Class_Initialize  dim RequestData,sStart,vbCrlf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,theFile  dim iFileSize,sFilePath,sFileType,sFormvalue,sFileName  dim iFindStart,iFindEnd  dim iFormStart,iFormEnd,sFormName  Version="化境HTTP上传程序 Version 2.0"  Dim ReadBytes,TrunkBytes,TotalBytes  ReadBytes = 0  TrunkBytes = 52428800     '--分段大小为50M,某位大大经过测试,无组件上传限制单个文件最大约≤65MB  TotalBytes = Request.TotalBytes  if TrunkBytes > TotalBytes Then TrunkBytes = TotalBytes  set objForm=Server.CreateObject("scripting.Dictionary")  objForm.CompareMode = 1  set objFile=Server.CreateObject("scripting.Dictionary")  objFile.CompareMode = 1  if Request.TotalBytes<1 then Exit Sub  set tStream = Server.CreateObject("adodb.stream")  set Data_5xsoft = Server.CreateObject("adodb.stream")  Data_5xsoft.Type = 1  Data_5xsoft.Mode =3  Data_5xsoft.Open  Do While ReadBytes < TotalBytes       Data_5xsoft.Write Request.BinaryRead(TrunkBytes)

            ReadBytes = ReadBytes + TrunkBytes

            If (ReadBytes + TrunkBytes) > TotalBytes Then TrunkBytes = TotalBytes mod TrunkBytes  '最后剩下的字节

        Loop  Data_5xsoft.Position=0  RequestData =Data_5xsoft.Read

      iFormStart = 1  iFormEnd =  Data_5xsoft.size  vbCrlf = chrB(13) & chrB(10)  sStart = MidB(RequestData,1, InStrB(iFormStart,RequestData,vbCrlf)-1)  iStart = LenB (sStart)  iFormStart=iFormStart+iStart+1  while (iFormStart + 10) < iFormEnd    iInfoEnd = InStrB(iFormStart,RequestData,vbCrlf & vbCrlf)+3    tStream.Type = 1    tStream.Mode =3    tStream.Open    Data_5xsoft.Position = iFormStart    Data_5xsoft.CopyTo tStream,iInfoEnd-iFormStart    tStream.Position = 0    tStream.Type = 2    tStream.Charset ="gb2312"    sInfo = tStream.ReadText    tStream.Close    '取得表单项目名称    iFormStart = InStrB(iInfoEnd,RequestData,sStart)    iFindStart = InStr(22,sInfo,"name=""",1)+6    iFindEnd = InStr(iFindStart,sInfo,"""",1)    sFormName = lcase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))    '如果是文件    if InStr (45,sInfo,"filename=""",1) > 0 then        set theFile=new FileInfo        '取得文件名        iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10        iFindEnd = InStr(iFindStart,sInfo,"""",1)        sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)        theFile.FileName=getFileName(sFileName)        theFile.FilePath=getFilePath(sFileName)        '取得文件类型        iFindStart = InStr(iFindEnd,sInfo,"Content-Type: ",1)+14        iFindEnd = InStr(iFindStart,sInfo,vbCr)        theFile.FileType =Mid (sinfo,iFindStart,iFindEnd-iFindStart)        theFile.FileStart =iInfoEnd        theFile.FileSize = iFormStart -iInfoEnd -3        theFile.FormName=sFormName        if not objFile.Exists(sFormName) then          objFile.add sFormName,theFile        end if    else    '如果是表单项目        tStream.Type =1        tStream.Mode =3        tStream.Open        Data_5xsoft.Position = iInfoEnd        Data_5xsoft.CopyTo tStream,iFormStart-iInfoEnd-3        tStream.Position = 0        tStream.Type = 2        tStream.Charset ="gb2312"            sFormvalue = tStream.ReadText            tStream.Close        if objForm.Exists(sFormName) then          objForm(sFormName)=objForm(sFormName)&", "&sFormvalue                  else          objForm.Add sFormName,sFormvalue        end if    end if    iFormStart=iFormStart+iStart+1    wend  RequestData=""  set tStream =nothingEnd Sub

    Private Sub Class_Terminate  if Request.TotalBytes>0 then    objForm.RemoveAll    objFile.RemoveAll    set objForm=nothing    set objFile=nothing    Data_5xsoft.Close    set Data_5xsoft =nothingend ifEnd Sub  Private function GetFilePath(FullPath)  If FullPath <> "" Then   GetFilePath = left(FullPath,InStrRev(FullPath, "//"))  Else   GetFilePath = ""  End IfEnd  function

    Private function GetFileName(FullPath)  If FullPath <> "" Then   GetFileName = mid(FullPath,InStrRev(FullPath, "//")+1)  Else   GetFileName = ""  End IfEnd  functionEnd Class

    Class FileInfo  dim FormName,FileName,FilePath,FileSize,FileType,FileStart  Private Sub Class_Initialize    FileName = ""    FilePath = ""    FileSize = 0    FileStart= 0    FormName = ""    FileType = ""  End Sub  Public function SaveAs(FullPath)    dim dr,ErrorChar,i    SaveAs=true    if trim(fullpath)="" or FileStart=0 or FileName="" or right(fullpath,1)="/" then exit function    set dr=CreateObject("Adodb.Stream")    dr.Mode=3    dr.Type=1    dr.Open    Data_5xsoft.position=FileStart    Data_5xsoft.copyto dr,FileSize    dr.SaveToFile FullPath,2    dr.Close    set dr=nothing    SaveAs=false  end function  End Class</script>

     

       

    2.   index.asp

    <form name="cn_bruce" method="POST" action=""><textarea cols="100" name="cn_content" rows="8" width="100%"></textarea><br></form><iframe border="0" frameBorder="0" noResize scrolling="no" width="100%" src="upload.asp"></iframe>

     

     

    3.upload.asp

     

        <meta http-equiv="content-type" content="text/html;charset=gb2312"><form name="form" method="post" action="upfile.asp" enctype="multipart/form-data"><input type="hidden" name="upfilefolder" value="uploadfile"><input type="hidden" name="act" value="upload"><input type="file" name="file11" size=40><input type="file" name="file12" size=40><input type="submit" name="Submit" value="上传">

    </form>

     

    4.  upfile.asp

    <%@LANGUAGE="VBscript" CODEPAGE="936"%><!--#include file="upload.inc"--><html><head><title>文件上传</title><meta http-equiv="content-type" content="text/html;charset=gb2312"></head><body><%Server.ScriptTimeout=9999

    dim upload,f_folder,file,formPath,iCount,filename,fileExt,filesizemin,filesizemaxresponse.write(server.mappath("M1"))filesizemin=100filesizemax=20*1024*1024*1024set upload=new upload_5xSoft '建立上传对象f_folder=upload.form("upfilefolder")

    '********************************列出所有上传文件***************************************************For each formName in upload.objFileset file=upload.file(formName)If file.filesize>0 then

    If file.filesize<filesizemin Then        response.write "你上传的文件太小了 [ <a href=# οnclick=history.go(-1)>重新上传</a> ]"    ElseIf file.filesize>filesizemax then        response.write "文件大小超过了 "&filesizemax&"字节 限制 [ <a href=# οnclick=history.go(-1)>重新上传</a> ]"    End If

        '********************************检测文件类型****************************************************    fileExt=ucase(right(file.filename,4))    uploadsuc=false    Forum_upload="RAR|ZIP|SWF|JPG|PNG|GIF|DOC|TXT|CHM|PDF|ACE|MP3|WMA|WMV|MIDI|AVI|RM|RA|RMVB|MOV|XLS"    Forumupload=split(Forum_upload,"|")    for i=0 to ubound(Forumupload)        if fileEXT="."&trim(Forumupload(i)) then            uploadsuc=true            exit for        else            uploadsuc=false        end if    next    if uploadsuc=false then        response.write "文件格式不正确 [ <a href=# οnclick=history.go(-1)>重新上传</a> ]"        response.end    end if

        '********************************建立文件上传的目录文件夹****************************************    Set upf=Server.CreateObject("scripting.FileSystemObject")    If Err<>0 Then        Err.Clear        response.write("您的服务器不支持FSO")        response.end    End If    f_type= replace(fileExt,".","")    'f_name= year(now)&"-"&month(now)    'If upf.FolderExists(Server.MapPath(f_folder&"/"&f_type&"/"&f_name))=False Then        If upf.FolderExists(Server.MapPath(f_folder&"/"&f_type))=False Then            If upf.FolderExists(Server.MapPath(f_folder))=False Then                upf.CreateFolder Server.MapPath(f_folder)                upf.CreateFolder Server.MapPath(f_folder&"/"&f_type)                'upf.CreateFolder Server.MapPath(f_folder&"/"&f_type&"/"&f_name)            Else                upf.CreateFolder Server.MapPath(f_folder&"/"&f_type)                'upf.CreateFolder Server.MapPath(f_folder&"/"&f_type&"/"&f_name)            End If        Else            upf.CreateFolder Server.MapPath(f_folder&"/"&f_type)        End If    'End If    f_ftn=f_folder&"/"&f_type      'f_ftn=Server.MapPath("/")    Set upf=Nothing

        '********************************保存上传文件至文件夹*****************************************    randomize    ranNum=int(90000*rnd)+10000    name =split(file.filename,"/")    namefile=name(ubound(name))    filename=f_ftn&"/"&namefile    response.write(filename)    if file.filesize>filesizemin and file.filesize<filesizemax then    file.SaveAs Server.mappath(filename)   '保存文件        if f_type="JPG" or f_type="GIF" or f_type="PNG" then            response.write "<script>parent.cn_bruce.cn_content.value+='[img]"&filename&"[/img]'</script>"        ElseIf f_type="ZIP" or f_type="RAR" or f_type="DOC" or f_type="TXT" then            response.write "<script>parent.cn_bruce.cn_content.value+='"&filename&"'</script>"        'ElseIf        else            response.write "<script>parent.cn_bruce.cn_content.value+=' "&filename&" '</script>"        end if        iCount=iCount+1    end ifset file=nothingend ifnextset upload=nothing  '删除此对象

    response.write ("        ------------------"&iCount&" 个文件上传成功! <a href=# οnclick=history.go(-1)>继续上传</a>")

    %></body></html>

     

     


    最新回复(0)