shawl.qiu ASP 无组件上传类 1.0 Final 版

由于本程序太过庞大, 功能过于复杂, shawl.qiu 随时捉虫中.

最新版本 url: http://blog.csdn.net/btbtd/archive/2006/09/30/1309457.aspx

shawl.qiu ASP 无组件上传类 1.0 Final 版

主要功能:
1.0 final:
支持自定义上传文件类型
支持自定义上传文件的时间间隔, 单位为秒
支持自定义上传文件总大小限制, 单位为Mb
支持自定义单个文件总大小限制, 单位为Kb
支持检测特定文件类型包含恶意代码的功能
1.0 beta:
支持自定义上传N个文件
支持自定义文件名前缀
支持随机自定义文件名后缀
支持任何编码的中文文件名

后续版本待加功能:
暂无合适的功能需要增加.

说明:
为了方便使用, 把表单域也封装进class(类)
接下来的几版, 主要为增加功能, 等到鄙人觉得没有必要再增加功能的时候, 鄙人将会从改进效率入手.
欢迎提供意见, 鄙人 E-Mail:  shawl.qiu@gmail.com

Subject: shawl.qiu ASP 无组件上传类 1.0 Final 版
Author: shawl.qiu
Version: 1.0 final 版
Date: 2006-09-30
Blog:    http://blog.csdn.net/btbtd
E-Mail:  shawl.qiu@gmail.com

版权声明: 本程序允许非商业用途的自由传播, 商业用途使用者必须支付作者著作权费, 人民币壹仟圆整.

目录: 
1. shawl.qiu ASP 无组件上传类 1.0 Final 版(cUpload)源代码及演示
2. 上传预览

shawl.qiu
2006-09-30
http://blog.csdn.net/btbtd

1. shawl.qiu ASP 无组件上传类 1.0 Final 版(cUpload)源代码及演示
  1. linenum
  2. <%
  3.     dim upload
  4.     set    upload=new cUpload
  5.         with upload
  6.             '----------------------------------------
  7.             ' shawl.qiu ASP 无组件上传类 1.0 Final 版
  8.             '----------------------------------------
  9.             .header '头部信息 [非必须]
  10.             '----------------------------------------
  11.             ' 页面设置:
  12.             '--------------------
  13.             .upQueryString=request.queryString("id") '目标查询 ID [必选项]
  14.             .upCharSet="utf-8" '页面编码 [必选项], 请正确设置页面编码! 
  15.             '----------------------------------------
  16.             ' 上传文件各参数设置:
  17.             '--------------------
  18.             .upBinaryTotal=cLng(request.TotalBytes) '读取二进制字节长度  [必选项]
  19.             .upPath="/saveimg/" ' 上传文件默认存放路径 [必选项]
  20.             ' 定义允许上传的文件扩展名, 以竖杠分隔("|")多个扩展名 [必选项]
  21.             .upFileNameExtension=".7z|.rar|.zip|.mp3|.bmp|.gif|.jpg|.jpeg|.png|.txt|.swf" 
  22.             .upAllowTotalSize=10 '允许上传的总大小, 单位为 MB(megabyte) [非必须]
  23.             .upAllowFileSize=1024 '允许上传的单个文件大小, 单位为 KB(kilobyte) [非必须]
  24.             .upOverWrite=2 ' 上传文件是否覆盖原有文件设置. 1 或空为不覆盖, 2 为覆盖, 默认为 1 [非必须]
  25.             .upTimeInterval=60 ' 允许上传文件的时间间隔, 单位为秒(Session) [非必须]
  26.             .upGoBackSec=60 ' 上传完毕后, 隔 N 秒后返回 [非必须]
  27.             .upCheckFile=".gif|.jpg|.jpeg|.png|.bmp" ' 指定检查是否包含恶意代码的文件类型 [非必须]
  28.             '----------------------------------------
  29.             ' 表单域默认值:
  30.             '--------------------
  31.             ' 提交表单目标地址, 处理上传操作 ID值必须为 upfile, 如 ?xxx=upfile [必选项]
  32.             .upAction="?id=upfile" 
  33.             .upNumber= 10 ' 显示上传文件 文件框 数量. 最小为1, 最大为 99, 默认为 1 [非必须]
  34.             .upFileNamePrefix="前缀" ' 自定义文件名前缀, 不想加前缀, 注释掉此项 [非必须]
  35.             .upFileNamePostfix=1 ' 是否添加随机后缀, 空值不添加, 注释掉此项 [非必须]
  36.             '----------------------------------------
  37.             .pgUpFile ' 处理上传文件操作 [必选项]
  38.             .pgUploadInfo ' 表单域内容相关信息及已上传文件相关信息 [非必须]
  39.             .pgUpForm ' 上传文件表单域 [必选项]
  40.             .pgShowAcceptExtension '显示允许上传的文件扩展名 [非必须]
  41.             .pgAcceptSize '显示允许上传总大小值(Mb), 和每文件最大允许值(Kb). [非必须]
  42.             '----------------------------------------
  43.             ' 文件上传操作相关信息:
  44.             'response.write join(.upFilePathArray,"<br/>") ' 存储所有已上传文件路径的数组 [非必须]
  45.             '.pgShowImage ' 显示已上传的图片文件 [非必须]
  46.             
  47.             .info ' 功能信息 [非必须]
  48.             .auther ' 作者及版本信息 [非必须]
  49.             '----------------------------------------
  50.         end with
  51.     set upload=nothing    
  52.     
  53.     class cUpload
  54.         private sub class_initialize()
  55.             response.write "<style type=""text/css"">/*<![CDATA[*/"
  56.             response.write "html,body{overflow-x: hidden;}"
  57.             response.write ".cAlCenter{text-align:center!important;}"
  58.             response.write ".cDspTable{distable:table;}"
  59.             response.write ".cDspBlock{distable:block;}"
  60.             response.write ".cCorRed{color:red!important;}"
  61.             response.write ".cW100pc{width:100%;}"
  62.             response.write ".cFtSize28px{font-size:28px;}"
  63.             response.write ".cMrg10px{margin:10px!important;}"
  64.             response.write ".cMrg0px{margin:0px!important;}"
  65.             response.write ".cPad0px{padding:0px!important;}"
  66.             response.write ".cPad10px{padding:10px!important;}"
  67.             response.write ".cPad0px_10px{padding:0px 10px!important;}"
  68.             response.write ".cPad10px_0px{padding:10px 0px!important;}"
  69.             response.write ".cLnHeight120pc{line-height: 120%;}"
  70.             response.write ".cLnHeight150pc{line-height: 150%;}"
  71.             response.write "/*]]>*/</style>"
  72.             
  73.             upHrf=request.ServerVariables("HTTP_REFERER")
  74.             upShowImageExtension=".jpg|.gif|.png|.jpeg|.bmp"
  75.             upFieldPrxPoxNum=0
  76.         end sub
  77.         
  78.         private sub class_terminate()
  79.         end sub
  80.         
  81.         public sub header()
  82.             response.write "<div class=""cAlCenter cDspTable cCorRed cW100pc cFtSize28px"">"
  83.             response.write "shawl.qiu ASP 无组件上传类 1.0 Final 版"
  84.             response.write "</div>"
  85.         end sub
  86.         
  87.         public sub info()
  88.             response.write "<div class=""cPad10px_0px""><h2 class=""cMrg0px cPad0px"">shawl.qiu ASP 无组件上传类 1.0 Final 版</h2><br/>"
  89.             response.write "主要功能:<br/>"
  90.             response.write "1.0 final:<br/><font class=""cCorRed"">"
  91.             response.write "支持自定义上传文件类型<br/>"
  92.             response.write "支持自定义上传文件的时间间隔, 单位为秒<br/>"
  93.             response.write "支持自定义上传文件总大小限制, 单位为Mb<br/>"
  94.             response.write "支持自定义单个文件总大小限制, 单位为Kb<br/>"
  95.             response.write "支持检测特定文件类型包含恶意代码的功能<br/></font>"
  96.             response.write "1.0 beta:<br/><font class=""cCorRed"">"
  97.             response.write "支持自定义上传N个文件<br/>"
  98.             response.write "支持自定义文件名前缀<br/>"
  99.             response.write "支持随机自定义文件名后缀<br/>"
  100.             response.write "支持任何编码的中文文件名<br/></font>"
  101.             response.write "<p/>后续版本待加功能:<br/><font class=""cCorRed"">"
  102.             response.write "暂无合适的功能需要增加.</font>"
  103.             response.write "<p/>说明:<br/><font class=""cCorRed"">"
  104.             response.write "为了方便使用, 把表单域也封装进class(类)<br/>"
  105.             response.write "接下来的几版, 主要为增加功能, 等到鄙人觉得没有必要再增加功能的时候, 鄙人将会从改进效率入手.<br/>"
  106.             response.write "欢迎提供意见, 鄙人 E-Mail: <a href=""mailto:shawl.qiu@gmail.com"">shawl.qiu@gmail.com</a></font>"
  107.             response.write "</div>"
  108.         end sub
  109.         
  110.         public sub auther()
  111.             response.write "<div class=""cPad10px_0px"">"
  112.             response.write "Subject: shawl.qiu ASP 无组件上传类 1.0 Final 版<br/>"
  113.             response.write "Author: shawl.qiu<br/>"
  114.             response.write "Version: 1.0 final 版<br/>"
  115.             response.write "Date: 2006-09-30<br/>"
  116.             response.write "Blog: <a href="" http://blog.csdn.net/btbtd"" target=""_blank""> http://blog.csdn.net/btbtd</a><br/>"
  117.             response.write "E-Mail: <a href=""mailto:shawl.qiu@gmail.com"">shawl.qiu@gmail.com</a><p/>"
  118.             response.write "版权声明: 本程序允许非商业用途的自由传播, 商业用途使用者必须支付作者著作权费, 人民币壹仟圆整."
  119.             response.write "</div>"
  120.         end sub
  121.         
  122.         public upQueryString, upCharSet
  123.         public upAction, upNumber, upFileNamePrefix, upFileNamePostfix
  124.         public upPath, upOverWrite, upBinaryTotal
  125.         public upFilePathArray, upFileNameExtension
  126.         public upTimeInterval
  127.         public upAllowTotalSize, upAllowFileSize
  128.         public upGoBackSec
  129.         public upCheckFile
  130.         
  131.         private upTotalFiled, upAvailableField, upFileUploaded, upFileUploadingTotal
  132.         private upSourceNameArray, upRenamedArray
  133.         private upShowImageExtension
  134.         private upAllFileNameArray, upAbortUploadTotal, upUploadedFileNameArray
  135.         private upHrf
  136.         private upOutsizeDescArray, upOutsizeFilenameArray
  137.         private upFileInvalidDescArray, upFileInvalidArray
  138.         private upUploadedFileSize
  139.         private upAbortFileArray
  140.         private upFieldPrxPoxNum
  141.         
  142.         public property get pgUploadInfo
  143.             dim upFileUploadingTotalTemp, upAbortFileArrayTemp, upOutsizeDescArrayTemp, upFileInvalidDescArrayTemp
  144.             dim upAvailableFieldTemp: upAvailableFieldTemp=0
  145.             if upBinaryTotal<1 then exit property
  146.             if upFileUploaded="" then upFileUploaded=0
  147.             if upFileUploadingTotal="" then upFileUploadingTotal=0
  148.             if isArray(upAbortFileArray) then  upAbortFileArrayTemp=uBound(upAbortFileArray)+1  else upAbortFileArrayTemp=0
  149.             if isArray(upOutsizeDescArray) then  upOutsizeDescArrayTemp=uBound(upOutsizeDescArray)+1  else upOutsizeDescArrayTemp=0
  150.             if isArray(upFileInvalidDescArray) then  upFileInvalidDescArrayTemp=uBound(upFileInvalidDescArray)+1  else upFileInvalidDescArrayTemp=0
  151.             if upNumber="" then upNumber=1
  152.             
  153.             upFileUploadingTotalTemp=upFileUploaded+upAbortFileArrayTemp+upOutsizeDescArrayTemp+upFileInvalidDescArrayTemp
  154.             upAvailableFieldTemp=upAvailableFieldTemp+upFileUploadingTotalTemp
  155.  
  156.             if upFileUploadingTotalTemp<1 then exit property
  157.                 response.write "<p/>表单域共有 "&upNumber+2&"个 Field, "
  158.                 response.write "可用 Field "&upFileUploadingTotalTemp+upFieldPrxPoxNum&"个, "
  159.                 response.write "总上传文件数 "&upFileUploadingTotalTemp&"个, "
  160.                 response.write "已上传文件数 "&upFileUploaded&"个. <br/>"
  161.                 response.write "本次上传数据总大小约: "&round(upBinaryTotal/1024)&" Kb, 约合 "&_
  162.                 round(upBinaryTotal/1024/1024)&" Mb."
  163.             if upFileUploaded>0 then 
  164.                 dim i
  165.                 for i=0 to upFileUploaded-1
  166.                     response.write "<p/>文件 "&i+1&" 原文件名: <font class=""cCorRed"">"&upSourceNameArray(i)&"</font><br/>"
  167.                     response.write "上传后文件名:<font class=""cCorRed""> "&upRenamedArray(i)&"</font><br/>"
  168.                     response.write "文件路径: <a href="""&upFilePathArray(i)&""" target=""_blank"">"&upFilePathArray(i)&"</a></font><br/>"
  169.                     response.write "文件大小: <font class=""cCorRed"">"&upUploadedFileSize(i)&"</font>"
  170.                 next
  171.             end if
  172.                 upAbortUploadTotal=upFileUploadingTotal-upFileUploaded
  173.             if upAbortFileArrayTemp>0 then 
  174.                     response.write "<p/>被禁止上传的文件扩展名 有 "&upAbortFileArrayTemp&"个:"
  175.                     response.write "<font class=""cCorRed"">"
  176.                 dim temp
  177.                 dim temp_:temp_=1
  178.                 for each temp in upAbortFileArray
  179.                         response.write "<br/>文件 "&temp_&" : "&temp
  180.                         temp_=temp_+1
  181.                 next
  182.                     response.write "</font>"
  183.             end if
  184.             if isArray(upOutsizeDescArray) then
  185.                 response.write "<p/>超出单文件大小("&upAllowFileSize&"Kb)限制的文件有 "&uBound(upOutsizeDescArray)+1
  186.                 response.write "个: <br/><font class=""cCorRed"">"
  187.                 response.write join(upOutsizeDescArray,"<br/>")
  188.                 response.write "</font>"
  189.             end if
  190.             if isArray(upFileInvalidDescArray) then
  191.                 response.write "<p/>检测到包含恶意代码的文件有 "&uBound(upFileInvalidDescArray)+1
  192.                 response.write "个, 已被禁止上传: <br/><font class=""cCorRed"">"
  193.                 response.write join(upFileInvalidDescArray,"<br/>")
  194.                 response.write "</font>"
  195.             end if
  196.             pgUploadInfo=pgUploadInfo
  197.         end property
  198.         
  199.         public property get pgUpFile
  200.             pgUpFile=fOperate(upBinaryTotal)
  201.         end property
  202.         
  203.         public property get pgUpForm
  204.             pgUpForm=upForm(upAction, upNumber, upFileNamePrefix, upFileNamePostfix) 
  205.         end property 
  206.         
  207.         public property get pgShowAcceptExtension
  208.             response.write " 允许上传的文件类型: "
  209.             response.write upFileNameExtension 
  210.         end property 
  211.         
  212.         public property get pgAcceptSize
  213.             if upAllowFileSize="" and upAllowFileSize="" then exit property
  214.                 response.write "<br/>"
  215.             if upAllowFileSize<>"" then
  216.                 response.write "每次上传最大值为 "
  217.                 response.write upAllowTotalSize
  218.                 response.write "Mb"
  219.             end if
  220.             if upAllowFileSize<>"" then
  221.                 response.write " | 每个文件允许的最大值为 "
  222.                 response.write upAllowFileSize
  223.                 response.write "Kb"
  224.             end if
  225.                 response.write "<br/>"
  226.         end property 
  227.         
  228.         public property get pgShowImage
  229.             pgShowImage=upShowImage(upShowImageExtension, upFilePathArray) 
  230.         end property 
  231.         
  232.         private function upShowImage(extension, filepath_)
  233.             if not isArray(upFilePathArray) then exit function
  234.             dim extAr:extAr=split(extension,"|")
  235.             dim temp, temp_
  236.             for each temp in upFilePathArray
  237.                 for each temp_ in extAr
  238.                     if strComp(mid(temp,inStrRev(temp,".")),temp_,1)=0 then
  239.                         response.write "<a href="""
  240.                         response.write temp
  241.                         response.write """ target=""_blank""><img src="""
  242.                         response.write temp
  243.                         response.write """ alt=""shawl.qiu upload"" title=""shawl.qiu upload""/></a>"
  244.                     end if
  245.                 next
  246.             next
  247.         end function
  248.         
  249.         private function fOperate(upBinaryTotal)
  250.             if upBinaryTotal=0 or upBinaryTotal="" then exit function
  251.             if upQueryString<>"upfile" or upAction="" then exit function
  252.             if upFileNameExtension="" then exit function
  253.             
  254.             if upAllowTotalSize<>"" and isNumeric(upAllowTotalSize) and upAllowTotalSize>0 then
  255.                 dim upAllowTotalSizeTemp:upAllowTotalSizeTemp=upAllowTotalSize*1014*1024
  256.                 if upAllowTotalSizeTemp<upBinaryTotal then 
  257.                     response.write "<div class=""cAlCenter cDspTable cW100pc cPad10px cLnHeight120pc"">上传文件总大小为 "&_
  258.                     formatNumber(upBinaryTotal,0)
  259.                     response.write "字节, 约 "&formatNumber(fix(upBinaryTotal/1024),0)&"Kb/"&fix(upBinaryTotal/1024/1024)&"Mb<br/>"
  260.                     response.write "超过 "&formatNumber(upAllowTotalSizeTemp,0)&"字节/"&formatNumber(upAllowTotalSizeTemp/1024,0)&"Kb/"
  261.                     response.write upAllowTotalSize&"Mb 的限制, 此次上传被终止"
  262.                     if upGoBackSec="" then upGoBackSec=0
  263.                     if upGoBackSec>0 then
  264.                         response.write fPmt("<p/>"&upGoBackSec&" 秒后 "&fGoBack("返回")&" 还有 ", upGoBackSec,  "", upHrf)
  265.                     end if
  266.                     response.write "</div>"
  267.                     exit function
  268.                 end if
  269.             end if
  270.             
  271.             if upTimeInterval<>"" and isNumeric(upTimeInterval) and upTimeInterval>0 then
  272.                 dim tInterval
  273.                 
  274.                 if session("timestamp")="" then session("timestamp")=now()
  275.                     tInterval=dateDiff("s",now(),session("timestamp"))
  276.                     
  277.                 if  tInterval>0  then
  278.                     response.write "<div class=""cAlCenter cW100pc cPad10px cLnHeight150pc"">每次上传文件的时间间隔为 "
  279.                     response.write upTimeInterval&"秒, 请稍后再操作. "&fGoBack("返回")&"<br/>"
  280.                     call fPmt(" 还有 ", tInterval, " 后才可以再次上传.", upHrf)
  281.                     response.write "</div>"
  282.                     exit function
  283.                 else
  284.                     session("timestamp")=dateAdd("s",upTimeInterval,now())
  285.  
  286.  
  287.                 end if
  288.             end if
  289.             
  290.             if upOverWrite="" then upOverWrite=1
  291.             if upOverWrite<1 or upOverWrite>2 then upOverWrite=1
  292.             
  293.             dim lf:lf=chrB(13)&chrB(10) 
  294.             dim bRead ' 读取二进制流内容
  295.                 bRead=request.BinaryRead(upBinaryTotal)
  296.                         
  297.             dim fieldMarker ' 定义取二进制流 Field 分隔标记 (内容为二进制)
  298.                 fieldMarker=leftB(bRead,inStrB(bRead,chrB(13))-1)
  299.                 
  300.             dim headerMarker:headerMarker=leftB(bRead,instrB(bRead,chrB(32))-1)
  301.             
  302.             dim temp:temp=1
  303.             dim temp_
  304.             
  305.             dim temp1
  306.             dim temp1_
  307.             
  308.             dim headerTotal
  309.             dim headerAvailable:headerAvailable=0
  310.             
  311.             dim headerStartPsti, headerEndPsti, headerStr
  312.             
  313.             dim fieldStartPsti, fieldEndPsti
  314.             
  315.             dim fnPrefix, fnPostfix
  316.             do
  317.                 temp_=inStrB(temp, bRead, headerMarker)
  318.                 if temp_<>0 then
  319.                     temp=temp_+1
  320.                     
  321.                     temp1=inStrB(temp, bRead, lf&lf)
  322.                     temp1_=inStrB(temp1+5, bRead, fieldMarker)
  323.                     headerTotal=headerTotal+1
  324.     
  325.                     if inStrB(midB(bRead, temp1+4, 36),midB(fieldMarker,1))=0 then
  326.                         if isArray(headerStartPsti) then redim preserve headerStartPsti(headerAvailable) _
  327.                         else redim headerStartPsti(headerAvailable)
  328.                             headerStartPsti(headerAvailable)=temp-1
  329.                             
  330.                         if isArray(headerEndPsti) then redim preserve headerEndPsti(headerAvailable) else _
  331.                         redim headerEndPsti(headerAvailable)
  332.                             headerEndPsti(headerAvailable)=temp1+2-temp
  333.  
  334.                         if isArray(headerStr) then redim preserve headerStr(headerAvailable) else redim _
  335.                          headerStr(headerAvailable)
  336.                          
  337.                         headerStr(headerAvailable)=fBin2Str(midB(bRead,temp-1,temp1+2-temp),upCharSet)
  338.                         
  339.                         if isArray(fieldStartPsti) then redim preserve fieldStartPsti(headerAvailable) else redim  fieldStartPsti(headerAvailable)
  340.                         fieldStartPsti(headerAvailable)=temp1
  341.                         
  342.                         if isArray(fieldEndPsti) then redim preserve fieldEndPsti(headerAvailable) else redim  fieldEndPsti(headerAvailable)
  343.                         fieldEndPsti(headerAvailable)=temp1_
  344.                         
  345.                         if inStr(headerStr(headerAvailable),"filename")=0 and inStr(headerStr(headerAvailable),"; name=")<>0 then
  346.                             select case fRegExpSgl(headerStr(headerAvailable),true,true,true,"[/s/S]*?name/=/""(.*?)""[/s/S]*","$1")
  347.                                 case "fnPrefix"
  348.                                     if upCharSet="gb2312" then
  349.                                         fnPrefix=bTsGb2312(midB(bRead,temp1,temp1_-temp1))
  350.                                     else
  351.                                         fnPrefix=fBin2Str(midB(bRead,temp1,temp1_-temp1), upCharSet)
  352.                                     end if
  353.                                 case "fnPostfix"
  354.                                     if upCharSet="gb2312" then
  355.                                         fnPostfix=bTsGb2312(midB(bRead,temp1,temp1_-temp1))
  356.                                     else
  357.                                         fnPostfix=fBin2Str(midB(bRead,temp1,temp1_-temp1), upCharSet)
  358.                                     end if
  359.                             end select
  360.                         end if
  361.                             headerAvailable=headerAvailable+1
  362.                     end if
  363.                 else
  364.                     exit do
  365.                 end if
  366.             loop    
  367.             
  368.             upTotalFiled=headerTotal
  369.             upAvailableField=headerAvailable
  370.             
  371.             dim fileName, fileNamePrx, fileNamePox
  372.             dim i, i_:i_=0
  373.             dim i1:i1=0
  374.             dim i1_:i1_=0
  375.             dim fileNmaeSaveing
  376.             dim temp2 
  377.             dim temp3
  378.             dim temp4:temp4=0
  379.             dim upFieldPrxPoxNumTemp
  380.  
  381.             if upCheckFile<>"" then
  382.                 upCheckFile=split(upCheckFile,"|")
  383.                 dim checkAspMark, checkAspMark_
  384.                     checkAspMark=chrB(60)&chrB(37) '<%
  385.                     checkAspMark_=chrB(37)&chrB(62) '%/>
  386.                 dim checkScriptMark, checkScriptMark_ , checkScriptMark__
  387.                     checkScriptMark=chrB(115)&chrB(99)&chrB(114)&chrB(105)&chrB(112)&chrB(116)
  388.                     checkScriptMark_=chrB(83)&chrB(99)&chrB(114)&chrB(105)&chrB(112)&chrB(116)
  389.                     checkScriptMark__=chrB(83)&chrB(67)&chrB(82)&chrB(73)&chrB(80)&chrB(84)
  390.             end if 
  391.             for i=0 to uBound(headerStr)
  392.                 if inStr(headerStr(i),"fnPrefix")<>0 or inStr(headerStr(i),"fnPostfix")<>0 then
  393.                         upFieldPrxPoxNumTemp=fBin2Str(midB(bRead,fieldStartPsti(i)+3,fieldEndPsti(i)-fieldStartPsti(i)-3),upCharSet)
  394.                         upFieldPrxPoxNumTemp=fRegExpSgl(upFieldPrxPoxNumTemp,true,true,true,"[/s]+","")
  395.                     if upFieldPrxPoxNumTemp<>"" then
  396.                         upFieldPrxPoxNum=upFieldPrxPoxNum+1 
  397.                     end if
  398.                 end if
  399.                 if inStr(headerStr(i),"filename=")<>0 then
  400.                         fileName=fRegExpSgl(headerStr(i),true,true,true,"[/s/S]*filename/=""(.*?)""[/s/S]*","$1")
  401.                         if inStrRev(fileName,"/")<>0 then fileName=mid(fileName,inStrRev(fileName,"/")+1) 
  402.                         fileNamePrx=fRegExpSgl(fileName,true,true,true,"(.*?)/..*","$1")
  403.                         fileNamePox=fRegExpSgl(fileName,true,true,true,".*(/..*)","$1")
  404.                         
  405.                     if isArray(upAllFileNameArray) then redim preserve upAllFileNameArray(i) else redim  upAllFileNameArray(i) 
  406.                         upAllFileNameArray(i)=fileName
  407.                         
  408.                     if inStr(1,upFileNameExtension,fileNamePox,1)=0 then
  409.                         if isArray(upAbortFileArray) then redim preserve upAbortFileArray(temp4) else redim  upAbortFileArray(temp4) 
  410.                         upAbortFileArray(temp4)=fileName
  411.                         temp4=temp4+1
  412.                     end if
  413.                         
  414.                     for each temp2 in split(upFileNameExtension,"|")
  415.                         if strComp(temp2,fileNamePox,1)=0 then
  416.                             if upAllowFileSize<>"" and upAllowFileSize>0 and isNumeric(upAllowFileSize) then
  417.                                 dim upFileSize:upFileSize=fieldEndPsti(i)-fieldStartPsti(i)-3
  418.                                 dim upAllowFileSizeTemp:upAllowFileSizeTemp=upAllowFileSize*1024
  419.                                 if upAllowFileSizeTemp<upFileSize then
  420.             
  421.                                     if isArray(upOutsizeDescArray) then redim preserve upOutsizeDescArray(i1) else redim _
  422.                                     upOutsizeDescArray(i1) 
  423.                                     upOutsizeDescArray(i1)="文件 "&i1+1&": "&fileName&" -|- "&round(upFileSize/1024)&" Kb"
  424.                                     if isArray(upOutsizeFilenameArray) then redim preserve upOutsizeFilenameArray(i1) else _
  425.                                     redim  upOutsizeFilenameArray(i1) 
  426.                                     upOutsizeFilenameArray(i1)=fileName
  427.                                     i1=i1+1
  428.                                     exit for
  429.                                 end if
  430.                             end if
  431.                             
  432.                             if isArray(upCheckFile) then
  433.                                     temp3=midB(bRead,fieldStartPsti(i)+3,fieldEndPsti(i)-fieldStartPsti(i)-3)
  434.                                 if (inStrB(1, temp3, checkAspMark, 1)<>0 and inStrB(1, temp3, checkAspMark_, 1)<>0) or _
  435.                                 inStrB(1, temp3, checkScriptMark, 0)<>0 or inStrB(1, temp3, checkScriptMark_, 0)<>0 _
  436.                                 or inStrB(1, temp3, checkScriptMark__, 0)<>0  then 
  437.                                     if isArray(upFileInvalidDescArray) then redim preserve upFileInvalidDescArray(i1_) else redim _
  438.                                     upFileInvalidDescArray(i1_) 
  439.                                     upFileInvalidDescArray(i1_)="文件 "&i1_+1&": "&fileName
  440.                                     
  441.                                     if isArray(upFileInvalidArray) then redim preserve upFileInvalidArray(i1_) else redim _
  442.                                     upFileInvalidArray(i1_) 
  443.                                     upFileInvalidArray(i1_)=fileName
  444.                                     
  445.                                     i1_=i1_+1
  446.                                     exit for
  447.                                 end if
  448.                             end if
  449.                             
  450.                             if isArray(upSourceNameArray) then redim preserve upSourceNameArray(i_) else redim  upSourceNameArray(i_) 
  451.                             upSourceNameArray(i_)=fileName
  452.                             
  453.                             if isArray(upUploadedFileNameArray) then redim preserve upUploadedFileNameArray(i_) else redim  upUploadedFileNameArray(i_) 
  454.                                 upUploadedFileNameArray(i_)=fileName
  455.                         
  456.                             if fnPrefix<>"" then fileNamePrx=fnPrefix&fileNamePrx
  457.                             if fnPostfix<>"" then fileNamePrx=fileNamePrx&fGuid
  458.                                 fileName=fileNamePrx&fileNamePox
  459.                                 fileName=fRegExpSgl(fileName,true,true,true,"[/s]+","")
  460.                             
  461.                             if isArray(upRenamedArray) then redim preserve upRenamedArray(i_) else redim  upRenamedArray(i_) 
  462.                                 upRenamedArray(i_)=fileName
  463.                             
  464.                             if isArray(upFilePathArray) then redim preserve upFilePathArray(i_) else redim  upFilePathArray(i_) 
  465.                                 upFilePathArray(i_)=upPath&fileName
  466.                             
  467.                             if isArray(upUploadedFileSize) then redim preserve upUploadedFileSize(i_) else redim  upUploadedFileSize(i_) 
  468.                                 upUploadedFileSize(i_)=round(upFileSize/1024)&" Kb"
  469.                         
  470.                             call fBinSv2fl(bRead,fieldStartPsti(i)+3,fieldEndPsti(i)-fieldStartPsti(i)-3, upPath&fileName, upOverWrite)
  471.                                 upFileUploaded=upFileUploaded+1
  472.                             i_=i_+1
  473.                         end if
  474.                     next
  475.                         upFileUploadingTotal=upFileUploadingTotal+1
  476.                 end if
  477.             next    
  478.             if upGoBackSec="" then upGoBackSec=0
  479.             if upGoBackSec>0 then
  480.                 response.write fPmt("<div class=""cAlCenter cW100pc cPad10px cLnHeight150pc"">操作已完毕"&_
  481.                 ", "&upGoBackSec&" 秒后 "&fGoBack("返回")&"<br/>还有 ", upGoBackSec,  "</div>", upHrf)
  482.             end if
  483.         end function
  484.         
  485.         private function fBinSv2fl(bin, bStart, bEnd, filepath_, ovWrite)
  486.         '--------------------------------------
  487.         ' 截取二进制流保存为文件 By shawl.qiu
  488.         '   http://blog.csdn.net/btbtd
  489.         '---------------------------
  490.         ' sample call: call fBinSv2fl(bRead,fieldStartPsti(i)+3,fieldEndPsti(i)-fieldStartPsti(i)-3, fileName, 2)
  491.         '------------
  492.         ' 参数说明:
  493.         '---------
  494.         ' bin: 源二进制流
  495.         ' bStart: 截取二进制流的起始位置
  496.         ' bEnd: 截取二进制流的结束位置
  497.         ' filepath_: 保存文件的路径
  498.         ' ovWrite: 是否覆盖原有文件. 1: 不覆盖; 2. 覆盖
  499.         '--------------------------------------
  500.                 filepath_=server.MapPath(filepath_)
  501.             dim stm_, fromStm_ 
  502.             set stm_=createObject("adodb.stream")
  503.                 stm_.type=1 
  504.                 stm_.mode=3 
  505.                 stm_.open
  506.                 stm_.write bin 
  507.                 set fromStm_=createOBject("adodb.stream")
  508.                     with fromStm_
  509.                         .type=1
  510.                         .mode=3
  511.                         .open
  512.                         stm_.position = bStart 
  513.                         stm_.copyTo fromStm_, bEnd 
  514.                         .saveTofile filepath_, ovWrite
  515.                         .close
  516.                     end with
  517.                 set fromStm_=nothing
  518.                 stm_.close  'shawl.qiu code'
  519.             set stm_=nothing
  520.         end function
  521.     
  522.         private function fStr2Bin(str, charSet)
  523.         '--------------------------------------
  524.         ' 字符串转二进制函数 By shawl.qiu
  525.         '   http://blog.csdn.net/btbtd
  526.         '---------------------------
  527.         ' 参数说明:
  528.         '----------
  529.         ' str: 要转换成二进制的字符串
  530.         ' charSet: 字符串默认编码集, 如不指定, 则默认为 gb2312
  531.         '---------------
  532.         ' sample call: response.binaryWrite fStr2Bin(str, "utf-8")
  533.         '--------------------------------------
  534.             dim stm_ 
  535.             set stm_=createObject("adodb.stream")
  536.                 with stm_
  537.                     .type=2 
  538.                     if charSet<>"" then
  539.                         .charSet=charSet
  540.                     else
  541.                         .charSet="gb2312"
  542.                     end if
  543.                     .open
  544.                     .writeText str
  545.                     .Position = 0
  546.                     .type=1
  547.                     fStr2Bin=.Read
  548.                     .close
  549.                 end with 'shawl.qiu code'
  550.             set stm_=nothing
  551.         end function
  552.         
  553.         private function fBin2Str(str, charSet)
  554.         '--------------------------------------
  555.         ' 二进制转字符串函数 By shawl.qiu
  556.         '   http://blog.csdn.net/btbtd
  557.         '--------------------------
  558.         ' 参数说明:
  559.         '----------
  560.         ' str: 要转换成字符串的二进制数据
  561.         ' charSet: 字符串默认编码集, 如不指定, 则默认为 gb2312
  562.         '-------------
  563.         ' sample call: response.write fBin2Str(midB(fStr2Bin(str, "utf-8"),1),"utf-8")
  564.         '--------------------------------------
  565.         ' 注意: 二进制字符串必须先用 midB(binaryString,1) 读取(可自定读取长度).
  566.         '--------------------------------------
  567.             dim stm_ 
  568.             set stm_=createObject("adodb.stream")
  569.                 with stm_
  570.                     .type=2 
  571.                     .open
  572.                     .writeText str
  573.                     .Position = 0
  574.                     if charSet<>"" then
  575.                         .CharSet = charSet
  576.                     else 
  577.                         .CharSet = "gb2312"
  578.                     end if
  579.                         fBin2Str=.ReadText
  580.                     .close
  581.                 end with 'shawl.qiu code'
  582.             set stm_=nothing
  583.         end function
  584.         
  585.        private function bTsGb2312(bin)
  586.         '二进制转为 string | gb2312 编码
  587.             dim i, iByt, sByt, bLen:bLen=lenB(bin)
  588.             for i=1 to bLen
  589.                 sByt=midB(bin,i,1):iByt=ascB(sByt)
  590.                 if iByt<128 then
  591.                     bTsGb2312=bTsGb2312&chr(iByt)
  592.                 else
  593.                     bTsGb2312=bTsGb2312&chr(ascW(midB(bin,i+1,1)&sByt))
  594.                     i=i+1
  595.                 end if
  596.             next 'shawl.qiu code'
  597.         end function
  598.         
  599.         private function fRegExpSgl(str,glb,igc,mtl,pt,rpt)
  600.             dim re
  601.             set re=new RegExp
  602.                 re.global=glb
  603.                 re.ignoreCase=igc
  604.                 re.multiline=mtl
  605.                 
  606.                 re.pattern=pt
  607.                 fRegExpSgl=re.replace(str,rpt)
  608.             set re=nothing
  609.         end function 'shawl.qiu code'
  610.         
  611.         private function fGuid
  612.                 fGuid=mid(cstr(createObject("scriptlet.typeLib").GUID),2,36)
  613.         end function 'shawl.qiu code'
  614.         
  615.         private function upForm(upAction, upNumber, upFileNamePrefix, upFileNamePostfix) 
  616.         '-------------------------------------------
  617.         ' shalw.qiu ASP 无组件上传类, 表单域
  618.         '-----------------------------------
  619.         ' sample call: call upForm("?id=test", 5, "文件前缀", "")
  620.         '-------------------------------------------
  621.             if upNumber="" then upNumber=1
  622.             if upNumber<1 or upNumber>99 then upNumber=5
  623.             if upFileNamePostfix<>"" then upFileNamePostfix=" checked=""checked"" "
  624.             dim i_    
  625.                 response.write "<form action="""&upAction&""" method=""post"""&_
  626.                 " enctype=""multipart/form-data"" name=""upForm"" id=""upForm"">"
  627.             for i_=1 to upNumber  
  628.                 response.write "<div class=""upBrowser""><input type=""file"" name=""file"" "&_
  629.                 "class=""upButton"" /> </div>"
  630.             next
  631.             response.write  "<div class=""upPrefix"">自定义前缀: <input name=""fnPrefix"" type=""text"""&_
  632.             " value="""&upFileNamePrefix&""" class=""upPrefixButton""/></div>"
  633.             response.write "<div class=""upPostfix"">添加随机后缀: <input name=""fnPostfix"" "&_
  634.             "type=""checkbox"" value=""checkbox"""& upFileNamePostfix&"class=""upPostfixButton"" /> </div>"
  635.             response.write " <input type=""submit"" value=""Submit"" class=""upSubmit"" />"
  636.             response.write "</form>"
  637.         end function 
  638.         
  639.         private    function fPmt(prefix, second, postfix, url)
  640.         '---------------------------------------------------
  641.         ' ASP N 秒后转到 XXX 页 函数 By shawl.qiu
  642.         '---------------------------
  643.         ' call fPmt("还有 ",20," 转到首页 ","/")
  644.         '--------------------
  645.         ' 注: 脚本参考自 CSDN Blog 出错页
  646.         '---------------------------------------------------
  647.             response.write "<script type=""text/javascript"">"&vbcrlf
  648.             response.write "//<![CDATA["&vbcrlf
  649.             response.write "    var go="&second&"000;"&vbcrlf
  650.             response.write "    var timer=null;"&vbcrlf
  651.             response.write "    var endTime = new Date().getTime() + go ;"&vbcrlf
  652.             response.write "    function interval() {"&vbcrlf
  653.             response.write "        var n=(endTime-new Date().getTime())/1000;"&vbcrlf
  654.             response.write "        if(n<0) return;"&vbcrlf
  655.             response.write "        document.getElementById(""jumpTo"").innerHTML = n.toFixed(3);"&vbcrlf
  656.             response.write "        setTimeout(interval, 10);"&vbcrlf
  657.             response.write "    }"&vbcrlf
  658.             response.write "    window.οnlοad=function(){"&vbcrlf
  659.             response.write "        timer=setTimeout(""window.location.href='"&url&"'"", go);"&vbcrlf
  660.             response.write "        interval();"&vbcrlf
  661.             response.write "    }"&vbcrlf
  662.             response.write "//]]>"&vbcrlf
  663.             response.write "</script>"&vbcrlf
  664.             response.write "<div class=""absltMid"" ><meta http-equiv=""refresh"" content="""
  665.             response.write second
  666.             response.write ";URL="
  667.             response.write url
  668.             response.write """>"
  669.             response.write prefix
  670.             response.write "<span id=""jumpTo"">"&second&".000</span>秒"
  671.             response.write postfix
  672.             response.write "</div>" 'shawl.qiu code'
  673.         end function 
  674.         
  675.         private function fGoBack(prompt)
  676.             dim hrf:hrf=request.ServerVariables("HTTP_REFERER")
  677.             fGoBack="<a href="""&hrf&""">"&prompt&"</a>"
  678.         end function
  679.     end class
  680. %>
  681. <a href="">back</a>

2. 上传预览
shawl.qiu ASP 无组件上传类 1.0 Final 版
<script type="text/javascript"> // </script>
操作已完毕, 3000 秒后 返回
还有 返回
还有 3000.000
自定义前缀:
添加随机后缀:
允许上传的文件类型: .7z|.rar|.zip|.mp3|.bmp|.gif|.jpg|.png|.txt|.swf
每次上传最大值为 10Mb | 每个文件允许的最大值为 1024Kb

表单域共有 12个 Field, 可用 Field 12个, 总上传文件数 10个, 已上传文件数 5个.
本次上传数据总大小约: 2220 Kb, 约合 2 Mb.

文件 1 原文件名: a.bmp
上传后文件名: 前缀aCD5C0B4D-5DD4-4F07-AD2F-531C5F606526.bmp
文件路径: /saveimg/前缀aCD5C0B4D-5DD4-4F07-AD2F-531C5F606526.bmp
文件大小: 576 Kb

文件 2 原文件名: b.gif
上传后文件名: 前缀b4669CC12-13B5-4ACE-B026-D5DA1C3D90DD.gif
文件路径: /saveimg/前缀b4669CC12-13B5-4ACE-B026-D5DA1C3D90DD.gif
文件大小: 7 Kb

文件 3 原文件名: b.jpg
上传后文件名: 前缀bCA734C87-0B5C-426F-BA36-8B1768669624.jpg
文件路径: /saveimg/前缀bCA734C87-0B5C-426F-BA36-8B1768669624.jpg
文件大小: 4 Kb

文件 4 原文件名: b.png
上传后文件名: 前缀b3FFA8EC5-2001-4DDC-BCFE-3572E7EE13E4.png
文件路径: /saveimg/前缀b3FFA8EC5-2001-4DDC-BCFE-3572E7EE13E4.png
文件大小: 3 Kb

文件 5 原文件名: 63.rar
上传后文件名: 前缀63FDABF757-DD1E-4065-BD35-B7DD2E40D98F.rar
文件路径: /saveimg/前缀63FDABF757-DD1E-4065-BD35-B7DD2E40D98F.rar
文件大小: 13 Kb

被禁止上传的文件扩展名 有 2个:
文件 1 : New Microsoft Excel 工作表.xls
文件 2 : New Microsoft Word 文档.doc

超出单文件大小(1024Kb)限制的文件有 1个:
文件 1: Oh_Canada.mp3 -|- 1577 Kb

检测到包含恶意代码的文件有 2个, 已被禁止上传:
文件 1: c.gif
文件 2: d.gif

shawl.qiu ASP 无组件上传类 1.0 Final 版


主要功能:
1.0 final:
支持自定义上传文件类型
支持自定义上传文件的时间间隔, 单位为秒
支持自定义上传文件总大小限制, 单位为Mb
支持自定义单个文件总大小限制, 单位为Kb
支持检测特定文件类型包含恶意代码的功能
1.0 beta:
支持自定义上传N个文件
支持自定义文件名前缀
支持随机自定义文件名后缀
支持任何编码的中文文件名

后续版本待加功能:
暂无合适的功能需要增加.

说明:
为了方便使用, 把表单域也封装进class(类)
接下来的几版, 主要为增加功能, 等到鄙人觉得没有必要再增加功能的时候, 鄙人将会从改进效率入手.
欢迎提供意见, 鄙人 E-Mail: shawl.qiu@gmail.com

Subject: shawl.qiu ASP 无组件上传类 1.0 Final 版
Author: shawl.qiu
Version: 1.0 final 版
Date: 2006-09-30
Blog: http://blog.csdn.net/btbtd
E-Mail: shawl.qiu@gmail.com

版权声明: 本程序允许非商业用途的自由传播, 商业用途使用者必须支付作者著作权费, 人民币壹仟圆整.

back


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值