asp 批量同时上传文字和图片的实现。

先上代码:

表单部分:

[vb]  view plain copy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
  5. <title>批量添加</title>  
  6. <mce:script src="js/jquery-1.4.3.min.js" mce_src="js/jquery-1.4.3.min.js" type="text/javascript"></mce:script>  
  7. <mce:style><!--  
  8. #pic{ font-size:12px;}  
  9. #pic input{ border:1px solid #999}  
  10. --></mce:style><style mce_bogus="1">#pic{ font-size:12px;}  
  11. #pic input{ border:1px solid #999}</style>  
  12. <mce:script language="javascript" type="text/javascript"><!--  
  13. var id=0;//添加组件编号  
  14. var count=0;//添加组件总数  
  15.  function FileExtCheck(arg_str_filename,arg_int_mode) {  
  16.         if (arg_str_filename == "") { return; }  
  17.         //var enumAllowExt = "jpg,png,gif,jpeg,rar,zip,7z,doc,docx,ppt,pptx,xls,xlsx,txt,rtf";  
  18.         var enumAllowExt = "jpg,png,gif,jpeg,bmp";//在这里添加允许的文件格式(白名单)  
  19.         var enumDisallowExt = "asp;php;htm;html;exe;bat";//在这里添加不允许的文件格式(黑名单)  
  20.         var strExt = arg_str_filename.substr(arg_str_filename.lastIndexOf(".") + 1).toLowerCase();  
  21.         var enumExt;  
  22.         if (arg_int_mode == 1) {  
  23.             //白名单模式  
  24.             var arrExt = enumAllowExt.split(",");  
  25.             var intErr = 1;  
  26.             for (var i = 0; i < arrExt.length; i++) {  
  27.                 if (strExt == arrExt[i]) {intErr = 0;}  
  28.             }  
  29.             if (intErr == 1) {                  
  30.                 return false;  
  31.             }  
  32.         }  
  33.         if (arg_int_mode == 2) {  
  34.             //黑名单模式  
  35.             var arrExt = enumDisallowExt.split(",");  
  36.             var intErr = 0;  
  37.             for (var i = 0; i < arrExt.length; i++) {  
  38.                 if (strExt == arrExt[i]) { intErr = 1; }  
  39.             }  
  40.             if (intErr == 1) {                  
  41.                 return false;  
  42.             }  
  43.         }  
  44.           
  45.     }  
  46.   
  47. //判断文件格式  
  48. function jian_yan(n){  
  49.      if (FileExtCheck(n.value, 1) == false) {  
  50.                 var who2= n.cloneNode(false);   
  51.                 who2.οnchange= n.onchange;   
  52.                 n.parentNode.replaceChild(who2,n);   
  53.    
  54.                 alert("文件格式不在允许上传的范围中!" );  
  55.                 return false  
  56.             }  
  57. }  
  58.   
  59.   
  60.   
  61. //添加  
  62. function addpic(){  
  63.        if (($(".title:last").attr('value')=="" || $(".file:last").attr('value')=="") && count!=0)  
  64.         {  
  65.             alert("请先把前面的内容填完整");  
  66.             return;  
  67.         }  
  68.       
  69.             count+=1;  
  70.             $("#pic").append("<div class=added id=div"+id+">标题:<input class=title type=text>&nbsp;&nbsp;附件:<input        οnchange=jian_yan(this); class=file type=file>&nbsp;&nbsp;<input type=button class=menubackg value=删除 οnclick=delpic("+id+");> </div>");  
  71.             id+=1;  
  72.       
  73. }  
  74.   
  75. //删除按钮  
  76. function delpic(divid){  
  77. var a=document.getElementById("div"+divid);  
  78. if(a!=null){  
  79.     var xxx=a.removeNode(true);  
  80.     count--;  
  81.     }  
  82. }  
  83.   
  84.   
  85. //后期处理并提交表单  
  86. function bian_hao(){  
  87.     if ((($(".title:last").attr('value')=="" || $(".file:last").attr('value')=="") && count!=0) || count==0)  
  88.         {  
  89.             alert("标题或图片路径为空");  
  90.             return;  
  91.         }  
  92.     $('.added').each(function(i){  
  93.         $(this).find('.title').attr('name','title'+i);    
  94.         $(this).find('.file').attr('name','file'+i);  
  95.         });  
  96.     $('form').attr("action",$('form').attr('action')+count);  
  97.     document.form1.submit();  
  98. }  
  99. // --></mce:script>  
  100. </head>  
  101. <body topmargin="15" leftmargin="40">  
  102. <form action="admin_++_inDB.asp?count=" ENCTYPE="multipart/form-data" method="post" name="form1">  
  103. <div style=" position:relative; top:10px; left:20px; font-size:14px">当前位置:批量添加</div>  
  104. <div style="position:relative; top:20px; left:20px; text-align:left">  
  105. <table width=800 border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#B2CDD2">  
  106. <tr bgcolor="#FFFFFF">  
  107. <td width="160" align="center" bgcolor="#FFFFFF" style="width:160px; height:22px; font-size:14px; margin-left:10px; overflow:hidden">文 件:  
  108. <a href="javascript:void(0);" mce_href="javascript:void(0);" onClick="addpic();"><img border="0" src="images/add.jpg" mce_src="images/add.jpg"></a></td>  
  109. <td id="pic"></td>  
  110. </tr>  
  111. <tr bgcolor="#F5FCFE">  
  112. <td style="font-size:12px; width:250px; overflow:hidden">支持.jpg/.jpeg/.png/.gif/.bmp图片上传</td>  
  113. <td>  
  114. <input  type="button" name="bname1" class="menubackg" value="确定添加" οnclick="bian_hao();">  
  115. <input type="button" name="bname3" class="menubackg" onClick="javascirpt:history.back();"  value=" 返回 ">  
  116. </td>  
  117. </tr>  
  118. </table>  
  119. </div>  
  120. </form>  
  121. </body>                                                                                   
  122. </html>  

 

数据库部分:

 

[vb]  view plain copy
  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>  
  2. <!--#include file="upload_class.asp"-->  
  3. <!--#include file="../inc/conn.asp"-->  
  4. <%  
  5. Dim Upload,path,tempCls,e  
  6. '===============================================================================  
  7. set Upload=new AnUpLoad                                                       '创建类实例  
  8. Upload.SingleSize=clng(1000 * 1024) * 1024                                    '设置单个文件最大上传限制,按字节计;默认为不限制  
  9. Upload.MaxSize=clng(1000 * 1024) * 1024                                       '设置最大上传限制,按字节计;默认为不限制  
  10. Upload.Exe="*"                                                                '设置合法扩展名,以|分割,忽略大小写  
  11. Upload.Charset="gb2312"                                                       '设置文本编码,默认为gb2312  
  12. Upload.GetData()                                                              '获取并保存数据,必须调用本方法  
  13. '===============================================================================  
  14. dim i  
  15. Dim title  
  16. Dim imgsrc  
  17. i = Trim(Request.QueryString("count"))  
  18. For d=0 To (i-1)  
  19. if Upload.ErrorID>0 then                                                      '判断错误号,如果myupload.Err<=0表示正常  
  20.         response.Write Upload.description  
  21. else  
  22.         if Upload.files(-1).count>0 then                                      '这里判断你是否选择了文件  
  23.             path=server.mappath("../mc")                                      '文件保存路径(这里是files文件夹)  
  24.             title=Upload.forms("title"&d)  
  25.             response.Write "标题:" & Upload.forms("title"&d)&"<br />"  
  26.             set tempCls=Upload.files("file"&d)   
  27.                 Set e = tempCls.SaveToFile(path,0)  
  28.                 if e.error then  
  29.                         response.Write e.description  
  30.                 else  
  31.                         imgsrc="/mc/"&tempCls.FileName  
  32.                         response.write "文件:"&tempCls.LocalName &"上传完毕!<br />"  
  33.                 End If  
  34.                 set tempCls=nothing       
  35. else   
  36.         if Upload.files(-1).count<=0 then   
  37.             response.Write("请选择文件")  
  38.         End if  
  39.         End if  
  40. End if  
  41. sql="select * from images"  
  42. Call Opendb  
  43. set rs=server.CreateObject("ADODB.recordset")  
  44. rs.open sql,conn,1,3  
  45. rs.AddNew  
  46. rs("title")=title  
  47. rs("imgsrc")=imgsrc  
  48. rs.Update  
  49. If Err <> 0 Then  
  50.     Err.Clear  
  51.     Response.Write"数据库添加失败<br />"  
  52.     Close conn  
  53. Else  
  54.     Err.Clear  
  55.     Response.Write "数据库添加成功<br />"  
  56.     Close conn  
  57. End If  
  58. rs.Close  
  59.   
  60. Next  
  61.   
  62. Response.End  
  63. set Upload=nothing                   '销毁类实例  
  64. %>  

 

无组件上传类:upload_class.asp

 

 

[vb]  view plain copy
  1. <%  
  2. '=========================================================  
  3.  '类名: AnUpLoad(艾恩无组件上传类)  
  4.  '作者: Anlige  
  5.  '版本: 艾恩ASP无组件上传类优化版(V9.11.1)  
  6.  '开发日期: 2008-4-12  
  7.  '修改日期: 20010-5-24  
  8.  '主页: http://dev.mo.cn  
  9.  'Email: i@ruboy.com  
  10.  'QQ: 1034555083  
  11. '=========================================================  
  12. Class AnUpLoad  
  13.     Private Form, Fils  
  14.     Private vCharSet, vMaxSize, vSingleSize, vErr, vVersion, vTotalSize, vExe, pID, vOP, vErrExe,vboundary, vLostTime, vMode, vFileCount  
  15.       
  16.     '==============================  
  17.     '设置和读取属性开始  
  18.     '==============================  
  19.     Public Property Let Mode(ByVal value)  
  20.         vMode = value  
  21.     End Property  
  22.       
  23.     Public Property Let MaxSize(ByVal value)  
  24.         vMaxSize = value  
  25.     End Property  
  26.       
  27.     Public Property Let SingleSize(ByVal value)  
  28.         vSingleSize = value  
  29.     End Property  
  30.       
  31.     Public Property Let Exe(ByVal value)  
  32.         vExe = LCase(value)  
  33.     End Property  
  34.       
  35.     Public Property Let CharSet(ByVal value)  
  36.         vCharSet = value  
  37.     End Property  
  38.       
  39.     Public Property Get ErrorID()  
  40.         ErrorID = vErr  
  41.     End Property  
  42.       
  43.     Public Property Get FileCount()  
  44.         FileCount = Fils.count  
  45.     End Property  
  46.       
  47.     Public Property Get Description()  
  48.         Description = GetErr(vErr)  
  49.     End Property  
  50.       
  51.     Public Property Get Version()  
  52.         Version = vVersion  
  53.     End Property  
  54.       
  55.     Public Property Get TotalSize()  
  56.         TotalSize = vTotalSize  
  57.     End Property  
  58.       
  59.     Public Property Get ProcessID()  
  60.         ProcessID = pID  
  61.     End Property  
  62.       
  63.     Public Property Let openProcesser(ByVal value)  
  64.         vOP = value  
  65.     End Property  
  66.       
  67.     Public Property Get LostTime()  
  68.         LostTime = vLostTime  
  69.     End Property  
  70.     '==============================  
  71.     '设置和读取属性结束,初始化类  
  72.     '==============================  
  73.       
  74.     Private Sub Class_Initialize()  
  75.         set Form = server.createobject("Scripting.Dictionary")  
  76.         set Fils = server.createobject("Scripting.Dictionary")  
  77.         vVersion = "艾恩ASP无组件上传类优化版(V9.11.1)"  
  78.         vMaxSize = -1  
  79.         vSingleSize = -1  
  80.         vErr = -1  
  81.         vExe = ""  
  82.         vTotalSize = 0  
  83.         vCharSet = "gb2312"  
  84.         vOP=false  
  85.         pID="AnUpload"  
  86.         setApp "",0,0,""  
  87.         vMode = 0  
  88.     End Sub  
  89.       
  90.     Private Sub Class_Terminate()  
  91.         Dim f  
  92.         Form.RemoveAll()  
  93.         For each f in Fils   
  94.             Fils(f).value=empty  
  95.             Set Fils(f) = Nothing  
  96.         Next  
  97.         Fils.RemoveAll()  
  98.         Set Form = Nothing  
  99.         Set Fils = Nothing  
  100.     End Sub  
  101.       
  102.     '==============================  
  103.     '函数名:GetData  
  104.     '作用:处理客户端提交来的所有数据  
  105.     '==============================  
  106.     Public Sub GetData()  
  107.         Dim time1  
  108.         time1 = timer()  
  109.         if vOP then pID=request.querystring("processid")  
  110.         Dim value, str, bcrlf, fpos, sSplit, slen, istart,ef  
  111.         Dim TotalBytes,tempdata,BytesRead,ChunkReadSize,PartSize,DataPart,formend, formhead, startpos, endpos, formname, FileName, fileExe, valueend, NewName,localname,type_1,contentType  
  112.         TotalBytes = Request.TotalBytes  
  113.         ef = false  
  114.         If checkEntryType = false Then ef = true : vErr = 2  
  115.         '下面3句注释掉了,因为在IIS5.0中,如果上传大小大于限制大小的文件,会出错,一直没找到解决方法。如果是在IIS5以上的版本使用,可以取消下面3句的注释  
  116.         'If Not ef Then  
  117.             'If vMaxSize > 0 And TotalBytes > vMaxSize Then ef = true : vErr = 1  
  118.         'End If  
  119.         If ef Then Exit Sub  
  120.         If vMode = 0 Then  
  121.             vTotalSize = 0  
  122.             Dim StreamT   
  123.             Set StreamT = server.CreateObject("Adodb.stream")  
  124.             StreamT.Type = 1  
  125.             StreamT.Mode = 3  
  126.             StreamT.Open  
  127.             BytesRead = 0  
  128.             ChunkReadSize = 1024 * 16  
  129.             Do While BytesRead < TotalBytes  
  130.                 PartSize = ChunkReadSize  
  131.                 If PartSize + BytesRead > TotalBytes Then PartSize = TotalBytes - BytesRead  
  132.                 DataPart = Request.BinaryRead(PartSize)  
  133.                 StreamT.Write DataPart  
  134.                 BytesRead = BytesRead + PartSize  
  135.                 setApp "uploading",TotalBytes,BytesRead,""  
  136.             Loop  
  137.             setApp "uploaded",TotalBytes,BytesRead,""  
  138.             StreamT.Position = 0  
  139.             tempdata = StreamT.Read  
  140.             StreamT.Close()  
  141.             Set StreamT = Nothing  
  142.         Else  
  143.             tempdata = Request.BinaryRead(TotalBytes)  
  144.         End If  
  145.         bcrlf = ChrB(13) & ChrB(10)  
  146.         fpos = InStrB(1, tempdata, bcrlf)  
  147.         sSplit = MidB(tempdata, 1, fpos - 1)  
  148.         slen = LenB(sSplit)  
  149.         istart = slen + 2  
  150.         Do While lenb(tempdata) > 2 + slen  
  151.             formend = InStrB(istart, tempdata, bcrlf & bcrlf)  
  152.             formhead = MidB(tempdata, istart, formend - istart)  
  153.             str = Bytes2Str(formhead)  
  154.             startpos = InStr(str, "name=""") + 6  
  155.             endpos = InStr(startpos, str, """")  
  156.             formname = LCase(Mid(str, startpos, endpos - startpos))  
  157.             valueend = InStrB(formend + 3, tempdata, sSplit)  
  158.             If InStr(str, "filename=""") > 0 Then  
  159.                 startpos = InStr(str, "filename=""") + 10  
  160.                 endpos = InStr(startpos, str, """")  
  161.                 type_1=instr(endpos,lcase(str),"content-type")  
  162.                 contentType=trim(mid(str,type_1+13))  
  163.                 FileName = Mid(str, startpos, endpos - startpos)  
  164.                 If Trim(FileName) <> "" Then  
  165.                     LocalName = FileName  
  166.                     FileName = Replace(FileName, "/""/")  
  167.                     FileName = Mid(FileName, InStrRev(FileName, "/") + 1)  
  168.                     FileName = Replace(FileName,chr(0),"")  
  169.                     If instr(FileName,".")>0 Then  
  170.                         fileExe = Split(FileName, ".")(UBound(Split(FileName, ".")))  
  171.                     else  
  172.                         fileExe = ""  
  173.                     End If  
  174.                     If vExe <> "" Then '判断扩展名  
  175.                         If checkExe(fileExe) = True Then  
  176.                             vErr = 3  
  177.                             vErrExe = fileExe  
  178.                             tempdata = empty  
  179.                             Exit Sub  
  180.                         End If  
  181.                     End If  
  182.                     NewName = Getname()  
  183.                     NewName = NewName & "." & fileExe  
  184.                     vTotalSize = vTotalSize + valueend - formend - 6  
  185.                     If vSingleSize > 0 And (valueend - formend - 6) > vSingleSize Then '判断上传单个文件大小  
  186.                         vErr = 5  
  187.                         tempdata = empty  
  188.                         Exit Sub  
  189.                     End If  
  190.                     If vMaxSize > 0 And vTotalSize > vMaxSize Then '判断上传数据总大小  
  191.                         vErr = 1  
  192.                         tempdata = empty  
  193.                         Exit Sub  
  194.                     End If  
  195.                     If Fils.Exists(formname) Then  
  196.                         vErr = 4  
  197.                         tempdata = empty  
  198.                         Exit Sub  
  199.                     Else  
  200.                         Dim fileCls:set fileCls=getNewFileObj()  
  201.                         fileCls.ContentType=contentType  
  202.                         fileCls.Size = (valueend - formend - 5)  
  203.                         fileCls.FormName = formname  
  204.                         fileCls.NewName = NewName  
  205.                         fileCls.FileName = FileName  
  206.                         fileCls.LocalName = FileName  
  207.                         fileCls.extend=split(NewName,".")(ubound(split(NewName,".")))  
  208.                         fileCls.value =midb(tempdata,formend + 4,valueend - formend - 5)  
  209.                         Fils.Add formname, fileCls  
  210.                         Set fileCls = Nothing  
  211.                     End If  
  212.                 End If  
  213.             Else  
  214.                 value = MidB(tempdata, formend + 4, valueend - formend - 6)  
  215.                 If Form.Exists(formname) Then  
  216.                     Form(formname) = Form(formname) & "," & Bytes2Str(value)  
  217.                 Else  
  218.                     Form.Add formname, Bytes2Str(value)  
  219.                 End If  
  220.             End If  
  221.             istart = 2 + slen  
  222.             tempdata = midb(tempdata,valueend+2)  
  223.         Loop  
  224.         vErr = 0  
  225.         tempdata = empty  
  226.         vLostTime = FormatNumber((timer-time1)*1000,2)  
  227.     End Sub  
  228.       
  229.     Public sub setApp(stp,total,current,desc)  
  230.         Application.lock()  
  231.         Application(pID)="{ID:""" & pID & """,step:""" & stp & """,total:" & total & ",now:" & current & ",description:""" & desc & """,dt:""" & now() & """}"  
  232.         Application.unlock()  
  233.     end sub  
  234.     '==============================  
  235.     '判断扩展名  
  236.     '==============================  
  237.     Private Function checkExe(ByVal ex)  
  238.         Dim notIn: notIn = True  
  239.         If vExe="*" then  
  240.             notIn=false   
  241.         elseIf InStr(1, vExe, "|") > 0 Then  
  242.             Dim tempExe: tempExe = Split(vExe, "|")  
  243.             Dim I: I = 0  
  244.             For I = 0 To UBound(tempExe)  
  245.                 If LCase(ex) = tempExe(I) Then  
  246.                     notIn = False  
  247.                     Exit For  
  248.                 End If  
  249.             Next  
  250.         Else  
  251.             If vExe = LCase(ex) Then  
  252.                 notIn = False  
  253.             End If  
  254.         End If  
  255.         checkExe = notIn  
  256.     End Function  
  257.       
  258.     '==============================  
  259.     '把数字转换为文件大小显示方式  
  260.     '==============================  
  261.     Public Function GetSize(ByVal Size)  
  262.         If Size < 1024 Then  
  263.             GetSize = FormatNumber(Size, 2) & "B"  
  264.         ElseIf Size >= 1024 And Size < 1048576 Then  
  265.             GetSize = FormatNumber(Size / 1024, 2) & "KB"  
  266.         ElseIf Size >= 1048576 Then  
  267.             GetSize = FormatNumber((Size / 1024) / 1024, 2) & "MB"  
  268.         End If  
  269.     End Function  
  270.       
  271.     '==============================  
  272.     '二进制数据转换为字符  
  273.     '==============================  
  274.     Private Function Bytes2Str(ByVal byt)  
  275.         If LenB(byt) = 0 Then  
  276.             Bytes2Str = ""  
  277.             Exit Function  
  278.         End If  
  279.         Dim mystream, bstr  
  280.         Set mystream =server.createobject("ADODB.Stream")  
  281.         mystream.Type = 2  
  282.         mystream.Mode = 3  
  283.         mystream.Open  
  284.         mystream.WriteText byt  
  285.         mystream.Position = 0  
  286.         mystream.CharSet = vCharSet  
  287.         mystream.Position = 2  
  288.         bstr = mystream.ReadText()  
  289.         mystream.Close  
  290.         Set mystream = Nothing  
  291.         Bytes2Str = bstr  
  292.     End Function  
  293.       
  294.     '==============================  
  295.     '获取错误描述  
  296.     '==============================  
  297.     Private Function GetErr(ByVal Num)  
  298.         Select Case Num  
  299.             Case 0  
  300.                 GetErr = "数据处理完毕!"  
  301.             Case 1  
  302.                 GetErr = "上传数据超过" & GetSize(vMaxSize) & "限制!可设置MaxSize属性来改变限制!"  
  303.             Case 2  
  304.                 GetErr = "未设置上传表单enctype属性为multipart/form-data或者未设置method属性为Post,上传无效!"  
  305.             Case 3  
  306.                 GetErr = "含有非法扩展名(" & vErrExe & ")文件!只能上传扩展名为" & Replace(vExe, "|"",") & "的文件"  
  307.             Case 4  
  308.                 GetErr = "对不起,程序不允许使用相同name属性的文件域!"  
  309.             Case 5  
  310.                 GetErr = "单个文件大小超出" & GetSize(vSingleSize) & "的上传限制!"  
  311.         End Select  
  312.     End Function  
  313.       
  314.     '==============================  
  315.     '根据日期生成随机文件名  
  316.     '==============================  
  317.     Private Function Getname()  
  318.         Dim y, m, d, h, mm, S, r  
  319.         Randomize  
  320.         y = Year(Now)  
  321.         m = Month(Now): If m < 10 Then m = "0" & m  
  322.         d = Day(Now): If d < 10 Then d = "0" & d  
  323.         h = Hour(Now): If h < 10 Then h = "0" & h  
  324.         mm = Minute(Now): If mm < 10 Then mm = "0" & mm  
  325.         S = Second(Now): If S < 10 Then S = "0" & S  
  326.         r = 0  
  327.         r = CInt(Rnd() * 1000)  
  328.         If r < 10 Then r = "00" & r  
  329.         If r < 100 And r >= 10 Then r = "0" & r  
  330.         Getname = y & m & d & h & mm & S & r  
  331.     End Function  
  332.       
  333.     '==============================  
  334.     '检测上传类型是否为multipart/form-data  
  335.     '==============================  
  336.     Private Function checkEntryType()  
  337.         Dim ContentType, ctArray, bArray,RequestMethod  
  338.         RequestMethod=trim(LCase(Request.ServerVariables("REQUEST_METHOD")))  
  339.         if RequestMethod="" or RequestMethod<>"post" then  
  340.             checkEntryType = False  
  341.             exit function  
  342.         end if  
  343.         ContentType = LCase(Request.ServerVariables("HTTP_CONTENT_TYPE"))  
  344.         ctArray = Split(ContentType, ";")  
  345.         if ubound(ctarray)>=0 then  
  346.             If Trim(ctArray(0)) = "multipart/form-data" Then  
  347.             checkEntryType = True  
  348.             vboundary = Split(ContentType,"boundary=")(1)  
  349.             Else  
  350.             checkEntryType = False  
  351.             End If  
  352.         else  
  353.             checkEntryType = False  
  354.         end if  
  355.     End Function  
  356.       
  357.     '==============================  
  358.     '获取上传表单值,参数可选,如果为-1则返回一个包含所有表单项的一个dictionary对象  
  359.     '==============================  
  360.     Public Function Forms(ByVal formname)  
  361.         If trim(formname) = "-1" Then  
  362.             Set Forms = Form  
  363.         Else  
  364.             If Form.Exists(LCase(formname)) Then  
  365.                 Forms = Form(LCase(formname))  
  366.             Else  
  367.                 Forms = ""  
  368.             End If  
  369.         End If  
  370.     End Function  
  371.       
  372.     '==============================  
  373.     '获取上传的文件类,参数可选,如果为-1则返回一个包含所有上传文件类的一个dictionary对象  
  374.     '==============================  
  375.     Public Function Files(ByVal formname)  
  376.         If trim(formname) = "-1" Then  
  377.             Set Files = Fils  
  378.         Else  
  379.             If Fils.Exists(LCase(formname)) Then  
  380.                 Set Files = Fils(LCase(formname))  
  381.             Else  
  382.                 Set Files = Nothing  
  383.             End If  
  384.         End If  
  385.     End Function  
  386. End Class  
  387. %>  
  388. <mce:script language="jscript" runat="server"><!--  
  389. function getNewFileObj(){  
  390.     return new UploadFile();      
  391. }  
  392. function UploadFile(){  
  393.     this.FormName="";  
  394.     this.NewName = "";  
  395.     this.LocalName="";  
  396.     this.FileName="";  
  397.     this.UserSetName="";  
  398.     this.ContentType="";  
  399.     this.Size=0;  
  400.     this.value=null;  
  401.     this.Path = "";  
  402.     this.extend="";  
  403. }  
  404.   
  405. //保存文件的方法  
  406. UploadFile.prototype.SaveToFile=function(){  
  407.     var arg = arguments;  
  408.     var Path ,Option, OverWrite  
  409.     if(arg.length==0){return {error:true,description:'参数错误,请传递至少一个参数'};}  
  410.     if(arg.length==1){Path = arg[0];Option=0;OverWrite=true;}  
  411.     if(arg.length==2){Path = arg[0];Option=arg[1];OverWrite=true;}  
  412.     if(arg.length==3){Path = arg[0];Option=arg[1];OverWrite=arg[2];}  
  413.     if(arg.length>3){return {error:true,description:'参数错误,最多传递3个参数'};}  
  414.     try{  
  415.         var IsP = (Path.indexOf(":")==1)  
  416.         if(!IsP){  
  417.             Path = server.MapPath(Path);      
  418.         }  
  419.         Path = Path.replace("/","//");  
  420.         if(Path.substr(Path.length-1,1)!="//"){Path = Path + "//";}  
  421.         this.CreateFolder(Path);  
  422.         this.Path = Path;  
  423.         if(Option==1){  
  424.             Path = Path + this.LocalName;this.FileName = this.LocalName;  
  425.         }else{  
  426.             if(Option==-1 && this.UserSetName!=""){  
  427.                 Path = Path + this.UserSetName + "." + this.extend;this.FileName = this.UserSetName + "." + this.extend;  
  428.             }else{  
  429.                 Path = Path + this.NewName;this.FileName = this.NewName;  
  430.             }  
  431.         }  
  432.         if(!OverWrite){  
  433.             Path = this.GetFilePath();  
  434.         }  
  435.         var tmpStrm;  
  436.         tmpStrm = server.CreateObject("adodb.stream");  
  437.         tmpStrm.mode=3;  
  438.         tmpStrm.type= 1;  
  439.         tmpStrm.open();  
  440.         var Info = server.CreateObject("ADODB.Recordset");  
  441.         Info.Fields.Append("value", 205,-1);  
  442.         Info.open();  
  443.         Info.addNew();  
  444.         Info("value").appendChunk(this.value);  
  445.         tmpStrm.write(Info("value"));  
  446.         Info("value").appendChunk(null);  
  447.         Info.update();  
  448.         Info.Close();  
  449.         Info = null;  
  450.         Path = Path.replace(//u0000/igm,"");  
  451.         tmpStrm.saveToFile(Path,2);  
  452.         tmpStrm.close();  
  453.         tmpStrm = null;  
  454.         return {error:false,description:'成功保存文件'};  
  455.     }catch(ex){  
  456.         return {error:true,description:ex.description};  
  457.     }  
  458. };  
  459. //获取二进制数据的方法  
  460. UploadFile.prototype.GetBytes=function(){  
  461.     return this.value  
  462. };  
  463.   
  464. UploadFile.prototype.CreateFolder=function(folderPath){  
  465.     var oFSO;  
  466.     oFSO =server.CreateObject("Scripting.FileSystemObject" );  
  467.     var sParent;  
  468.     sParent = oFSO.GetParentFolderName( folderPath );  
  469.     if(sParent == ""){return;}  
  470.     if(!oFSO.FolderExists(sParent)){this.CreateFolder( sParent );}  
  471.     if(!oFSO.FolderExists(folderPath)){oFSO.CreateFolder(folderPath);}  
  472.     oFSO = null;  
  473. };  
  474.   
  475. UploadFile.prototype.GetFilePath=function(){  
  476.     var oFSO,Fname,FNameL,i=0;  
  477.     oFSO =server.CreateObject("Scripting.FileSystemObject" );  
  478.     Fname = this.Path + this.FileName;  
  479.     FNameL = this.FileName.substr(0,this.FileName.lastIndexOf("."));  
  480.     while(oFSO.FileExists(Fname)){  
  481.         Fname = this.Path + FNameL + "(" + i + ")." + this.extend;  
  482.         this.FileName = FNameL + "(" + i + ")." + this.extend  
  483.         i++;  
  484.     }  
  485.     oFSO = null;  
  486.     return Fname;  
  487. };  
  488. // --></mce:script>  

 

conn.asp是数据库链接的可以自己写个

 

上图:

 

1:

 

 

 

2:

 

 3:

 

              

 

以上程序采用

同时上传图片和写入数据库。

更有利于图片(文件)的管理。

经过修改可以只上传1个图片(文件)和文字数据

或者控制文件上传最大数

有任何问题请加QQ1070452286~

感谢大家支持

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值