yns_upload.asp
上传用户:yjkj1008
上传日期:2020-10-17
资源大小:1496k
文件大小:15k
源码类别:

电子政务应用

开发平台:

HTML/CSS

  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> 
  2. <!--#include file="Connections/conn_news.asp" -->
  3. <%
  4. '请尊重作者劳动成果不要删除以上信息
  5. '业一新闻系统3.0正式版
  6. '主页:http://yeyi.net
  7. '论坛:http://bbs.yeyi.net
  8. '业一网络 承接各种网站制作
  9. '程序开发 软件开发业务
  10. '业务联系
  11. '电话: 13007310512
  12. '联系人:郝亚平
  13. 'QQ:24344842 (只谈业务,技术问题请访问论坛)
  14. '请尊重作者劳动成果不要删除以上信息
  15. %>
  16. <% dim m
  17. m=request("m")
  18. %> 
  19. <%
  20. ' *** Restrict Access To Page: Grant or deny access to this page
  21. MM_authorizedUsers="1,2,3"
  22. MM_authFailedURL="default.asp"
  23. MM_grantAccess=false
  24. If Session("MM_Username") <> "" Then
  25.   If (false Or CStr(Session("MM_UserAuthorization"))="") Or _
  26.          (InStr(1,MM_authorizedUsers,Session("MM_UserAuthorization"))>=1) Then
  27.     MM_grantAccess = true
  28.   End If
  29. End If
  30. If Not MM_grantAccess Then
  31.   MM_qsChar = "?"
  32.   If (InStr(1,MM_authFailedURL,"?") >= 1) Then MM_qsChar = "&"
  33.   MM_referrer = Request.ServerVariables("URL")
  34.   if (Len(Request.QueryString()) > 0) Then MM_referrer = MM_referrer & "?" & Request.QueryString()
  35.   MM_authFailedURL = MM_authFailedURL & MM_qsChar & "accessdenied=" & Server.URLEncode(MM_referrer)
  36.   Response.Redirect(MM_authFailedURL)
  37. End If
  38. %>
  39. <%
  40. '*** File Upload to: upload, Extensions: "GIF,JPG,JPEG,BMP,PNG", Form: form1, Redirect: "", "file", "1024000", "error"
  41. '*** Pure ASP File Upload Modify Version by xPilot-----------------------------------------------------
  42. ' Copyright 2000 (c) George Petrov
  43. '
  44. ' Script partially based on code from Philippe Collignon 
  45. '              (http://www.asptoday.com/articles/20000316.htm)
  46. '
  47. ' New features from GP:
  48. '  * Fast file save with ADO 2.5 stream object
  49. '  * new wrapper functions, extra error checking
  50. '  * UltraDev Server Behavior extension
  51. '
  52. ' Copyright 2001-2002 (c) Modify by xPilot
  53. ' *** Date: 12/15/2001 ***
  54. ' *** 支持所有双字节文件名,而且修复了原函数中遇到空格也会自动截断文件名的错误! ***
  55. ' *** 保证百分百以原文件名保存上传文件!***
  56. ' *** Welcome to visite pilothome.yeah.net or mail xpilot@21cn.com to me!***
  57. '
  58. ' Version: 2.0.1 Beta for GB2312,BIG5,Japan,Korea ...
  59. '------------------------------------------------------------------------------
  60. Sub BuildUploadRequest(RequestBin,UploadDirectory,storeType,sizeLimit,nameConflict)
  61.   'Get the boundary
  62.   PosBeg = 1
  63.   PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
  64.   if PosEnd = 0 then
  65.     Response.Write "<b>Form was submitted with no ENCTYPE=""multipart/form-data""</b><br>"
  66.     Response.Write "Please correct the form attributes and try again."
  67.     Response.End
  68.   end if
  69.   'Check ADO Version
  70. set checkADOConn = Server.CreateObject("ADODB.Connection")
  71. adoVersion = CSng(checkADOConn.Version)
  72. set checkADOConn = Nothing
  73. if adoVersion < 2.5 then
  74.     Response.Write "<b>You don't have ADO 2.5 installed on the server.</b><br>"
  75.     Response.Write "The File Upload extension needs ADO 2.5 or greater to run properly.<br>"
  76.     Response.Write "You can download the latest MDAC (ADO is included) from <a href=""www.microsoft.com/data"">www.microsoft.com/data</a><br>"
  77.     Response.End
  78. end if
  79.   'Check content length if needed
  80. Length = CLng(Request.ServerVariables("HTTP_Content_Length")) 'Get Content-Length header
  81. If "" & sizeLimit <> "" Then
  82.     sizeLimit = CLng(sizeLimit)
  83.     If Length > sizeLimit Then
  84.       Request.BinaryRead (Length)
  85.       Response.Write "Upload size " & FormatNumber(Length, 0) & "B exceeds limit of " & FormatNumber(sizeLimit, 0) & "B"
  86.       Response.End
  87.     End If
  88.   End If
  89.   boundary = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
  90.   boundaryPos = InstrB(1,RequestBin,boundary)
  91.   'Get all data inside the boundaries
  92.   Do until (boundaryPos=InstrB(RequestBin,boundary & getByteString("--")))
  93.     'Members variable of objects are put in a dictionary object
  94.     Dim UploadControl
  95.     Set UploadControl = CreateObject("Scripting.Dictionary")
  96.     'Get an object name
  97.     Pos = InstrB(BoundaryPos,RequestBin,getByteString("Content-Disposition"))
  98.     Pos = InstrB(Pos,RequestBin,getByteString("name="))
  99.     PosBeg = Pos+6
  100.     PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
  101.     Name = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
  102.     PosFile = InstrB(BoundaryPos,RequestBin,getByteString("filename="))
  103.     PosBound = InstrB(PosEnd,RequestBin,boundary)
  104.     'Test if object is of file type
  105.     If  PosFile<>0 AND (PosFile<PosBound) Then
  106.       'Get Filename, content-type and content of file
  107.       PosBeg = PosFile + 10
  108.       PosEnd =  InstrB(PosBeg,RequestBin,getByteString(chr(34)))
  109.       FileName = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
  110.       FileName = Mid(FileName,InStrRev(FileName,"")+1)
  111.       'Add filename to dictionary object
  112.       UploadControl.Add "FileName", FileName
  113.       Pos = InstrB(PosEnd,RequestBin,getByteString("Content-Type:"))
  114.       PosBeg = Pos+14
  115.       PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
  116.       'Add content-type to dictionary object
  117.       ContentType = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
  118.       UploadControl.Add "ContentType",ContentType
  119.       'Get content of object
  120.       PosBeg = PosEnd+4
  121.       PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
  122.       Value = FileName
  123.       ValueBeg = PosBeg-1
  124.       ValueLen = PosEnd-Posbeg
  125.     Else
  126.       'Get content of object
  127.       Pos = InstrB(Pos,RequestBin,getByteString(chr(13)))
  128.       PosBeg = Pos+4
  129.       PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
  130.       Value = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
  131.       ValueBeg = 0
  132.       ValueEnd = 0
  133.     End If
  134.     'Add content to dictionary object
  135.     UploadControl.Add "Value" , Value
  136.     UploadControl.Add "ValueBeg" , ValueBeg
  137.     UploadControl.Add "ValueLen" , ValueLen
  138.     'Add dictionary object to main dictionary
  139.     UploadRequest.Add name, UploadControl
  140.     'Loop to next object
  141.     BoundaryPos=InstrB(BoundaryPos+LenB(boundary),RequestBin,boundary)
  142.   Loop
  143.   GP_keys = UploadRequest.Keys
  144.   for GP_i = 0 to UploadRequest.Count - 1
  145.     GP_curKey = GP_keys(GP_i)
  146.     'Save all uploaded files
  147.     if UploadRequest.Item(GP_curKey).Item("FileName") <> "" then
  148.       GP_value = UploadRequest.Item(GP_curKey).Item("Value")
  149.       GP_valueBeg = UploadRequest.Item(GP_curKey).Item("ValueBeg")
  150.       GP_valueLen = UploadRequest.Item(GP_curKey).Item("ValueLen")
  151.       if GP_valueLen = 0 then
  152.         Response.Write "<B>An error has occured saving uploaded file!</B><br><br>"
  153.         Response.Write "Filename: " & Trim(GP_curPath) & UploadRequest.Item(GP_curKey).Item("FileName") & "<br>"
  154.         Response.Write "File does not exists or is empty.<br>"
  155.         Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
  156.      response.End
  157.     end if
  158.       
  159.       'Create a Stream instance
  160.       Dim GP_strm1, GP_strm2
  161.       Set GP_strm1 = Server.CreateObject("ADODB.Stream")
  162.       Set GP_strm2 = Server.CreateObject("ADODB.Stream")
  163.       
  164.       'Open the stream
  165.       GP_strm1.Open
  166.       GP_strm1.Type = 1 'Binary
  167.       GP_strm2.Open
  168.       GP_strm2.Type = 1 'Binary
  169.         
  170.       GP_strm1.Write RequestBin
  171.       GP_strm1.Position = GP_ValueBeg
  172.       GP_strm1.CopyTo GP_strm2,GP_ValueLen
  173.     
  174.       'Create and Write to a File
  175.       GP_curPath = Request.ServerVariables("PATH_INFO")
  176.       GP_curPath = Trim(Mid(GP_curPath,1,InStrRev(GP_curPath,"/")) & UploadDirectory)
  177.       if Mid(GP_curPath,Len(GP_curPath),1)  <> "/" then
  178.         GP_curPath = GP_curPath & "/"
  179.       end if 
  180.       GP_CurFileName = UploadRequest.Item(GP_curKey).Item("FileName")
  181.       GP_FullFileName = Trim(Server.mappath(GP_curPath))& "" & GP_CurFileName
  182.       'Check if the file alreadu exist
  183.       GP_FileExist = false
  184.       Set fso = CreateObject("Scripting.FileSystemObject")
  185.       If (fso.FileExists(GP_FullFileName)) Then
  186.         GP_FileExist = true
  187.       End If      
  188.       if nameConflict = "error" and GP_FileExist then
  189.         Response.Write "<B>File already exists!</B><br><br>"
  190.         Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
  191. GP_strm1.Close
  192. GP_strm2.Close
  193.      response.End
  194.       end if
  195.       if ((nameConflict = "over" or nameConflict = "uniq") and GP_FileExist) or (NOT GP_FileExist) then
  196.         if nameConflict = "uniq" and GP_FileExist then
  197.           Begin_Name_Num = 0
  198.           while GP_FileExist    
  199.             Begin_Name_Num = Begin_Name_Num + 1
  200.             GP_FullFileName = Trim(Server.mappath(GP_curPath))& "" & fso.GetBaseName(GP_CurFileName) & "_" & Begin_Name_Num & "." & fso.GetExtensionName(GP_CurFileName)
  201.             GP_FileExist = fso.FileExists(GP_FullFileName)
  202.           wend  
  203.           UploadRequest.Item(GP_curKey).Item("FileName") = fso.GetBaseName(GP_CurFileName) & "_" & Begin_Name_Num & "." & fso.GetExtensionName(GP_CurFileName)
  204. UploadRequest.Item(GP_curKey).Item("Value") = UploadRequest.Item(GP_curKey).Item("FileName")
  205.         end if
  206.         on error resume next
  207.         GP_strm2.SaveToFile GP_FullFileName,2
  208.         if err then
  209.           Response.Write "<B>An error has occured saving uploaded file!</B><br><br>"
  210.           Response.Write "Filename: " & Trim(GP_curPath) & UploadRequest.Item(GP_curKey).Item("FileName") & "<br>"
  211.           Response.Write "Maybe the destination directory does not exist, or you don't have write permission.<br>"
  212.           Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
  213.        err.clear
  214.    GP_strm1.Close
  215.    GP_strm2.Close
  216.         response.End
  217.        end if
  218.    GP_strm1.Close
  219.    GP_strm2.Close
  220.    if storeType = "path" then
  221.    UploadRequest.Item(GP_curKey).Item("Value") = GP_curPath & UploadRequest.Item(GP_curKey).Item("Value")
  222.    end if
  223.         on error goto 0
  224.       end if
  225.     end if
  226.   next
  227. End Sub
  228. '把普通字符串转成二进制字符串函数
  229. Function getByteString(StringStr)
  230.     getByteString=""
  231.   For i = 1 To Len(StringStr) 
  232.     XP_varchar = mid(StringStr,i,1)
  233.     XP_varasc = Asc(XP_varchar) 
  234.     If XP_varasc < 0 Then 
  235.        XP_varasc = XP_varasc + 65535 
  236.     End If 
  237.     If XP_varasc > 255 Then 
  238.        XP_varlow = Left(Hex(Asc(XP_varchar)),2) 
  239.        XP_varhigh = right(Hex(Asc(XP_varchar)),2) 
  240.        getByteString = getByteString & chrB("&H" & XP_varlow) & chrB("&H" & XP_varhigh) 
  241.     Else 
  242.        getByteString = getByteString & chrB(AscB(XP_varchar)) 
  243.     End If 
  244.   Next 
  245. End Function
  246. '把二进制字符串转换成普通字符串函数 
  247. Function getString(StringBin)
  248.    getString =""
  249.    Dim XP_varlen,XP_vargetstr,XP_string,XP_skip
  250.    XP_skip = 0 
  251.    XP_string = "" 
  252.  If Not IsNull(StringBin) Then 
  253.       XP_varlen = LenB(StringBin) 
  254.     For i = 1 To XP_varlen 
  255.       If XP_skip = 0 Then
  256.          XP_vargetstr = MidB(StringBin,i,1) 
  257.          If AscB(XP_vargetstr) > 127 Then 
  258.            XP_string = XP_string & Chr(AscW(MidB(StringBin,i+1,1) & XP_vargetstr)) 
  259.            XP_skip = 1 
  260.          Else 
  261.            XP_string = XP_string & Chr(AscB(XP_vargetstr)) 
  262.          End If 
  263.       Else 
  264.       XP_skip = 0
  265.    End If 
  266.     Next 
  267.  End If 
  268.       getString = XP_string 
  269. End Function 
  270. Function UploadFormRequest(name)
  271.   on error resume next
  272.   if UploadRequest.Item(name) then
  273.     UploadFormRequest = UploadRequest.Item(name).Item("Value")
  274.   end if  
  275. End Function
  276. 'Process the upload
  277. UploadQueryString = Replace(Request.QueryString,"GP_upload=true","")
  278. if mid(UploadQueryString,1,1) = "&" then
  279. UploadQueryString = Mid(UploadQueryString,2)
  280. end if
  281. GP_uploadAction = CStr(Request.ServerVariables("URL")) & "?GP_upload=true"
  282. If (Request.QueryString <> "") Then  
  283.   if UploadQueryString <> "" then
  284.   GP_uploadAction = GP_uploadAction & "&" & UploadQueryString
  285.   end if 
  286. End If
  287. If (CStr(Request.QueryString("GP_upload")) <> "") Then
  288.   GP_redirectPage = ""
  289.   If (GP_redirectPage = "") Then
  290.     GP_redirectPage = CStr(Request.ServerVariables("URL"))
  291.   end if
  292.     
  293.   RequestBin = Request.BinaryRead(Request.TotalBytes)
  294.   Dim UploadRequest
  295.   Set UploadRequest = CreateObject("Scripting.Dictionary")  
  296.   BuildUploadRequest RequestBin, "upload", "file", "1024000", "error"
  297.   
  298.   '*** GP NO REDIRECT
  299. end if  
  300. if UploadQueryString <> "" then
  301.   UploadQueryString = UploadQueryString & "&GP_upload=true"
  302. else  
  303.   UploadQueryString = "GP_upload=true"
  304. end if  
  305. %> 
  306.     <% if request.querystring ("GP_upload") <> "" then %>
  307.     <%
  308.     Dim objFSO,MMFilename,TimeMM,FileType,fname
  309. fname = now()
  310.   fname = replace(fname,"-","")
  311.   fname = replace(fname," ","") 
  312.   fname = replace(fname,":","")
  313.   fname = replace(fname,"PM","")
  314.   fname = replace(fname,"AM","")
  315.   fname = replace(fname,"上午","")
  316.   fname = replace(fname,"下午","")
  317.     MMFilename = UploadFormRequest("file") '获得上传文件名
  318.     FileType = Right(MMFilename,4) '获得上传文件的类型
  319.     TimeMM = fname&FileType '使用一个时间数值来确定文件名
  320. Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
  321.     If objFSO.FileExists(Server.MapPath("upload/"&MMFilename)) Then
  322.     objFSO.MoveFile Server.MapPath("upload/"&MMFilename), Server.MapPath("upload/"&TimeMM) '重命名文件
  323.     Else
  324.     Response.Write "产生错误,文件重命名失败!"
  325.     End If
  326.     Set objFSO = Nothing
  327.     %>
  328. <script>window.opener.form1.<% If m=1 Then %>n_mpic<% ElseIf m=2 Then %>n_rpic<% ElseIf m=3 Then %>n_content.value+='[img]../upload/<%=TimeMM%>[/img]';window.close();</script><% End If %>
  329. .value+='<%=TimeMM%>';window.close();</script>
  330.     <% End If %>
  331. <html>
  332. <head>
  333. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  334. <title></title>
  335. <link href="style/css.css" rel="stylesheet" type="text/css">
  336. <script language="JavaScript">
  337. <!--
  338. function getFileExtension(filePath) { //v1.0
  339.   fileName = ((filePath.indexOf('/') > -1) ? filePath.substring(filePath.lastIndexOf('/')+1,filePath.length) : filePath.substring(filePath.lastIndexOf('\')+1,filePath.length));
  340.   return fileName.substring(fileName.lastIndexOf('.')+1,fileName.length);
  341. }
  342. function checkFileUpload(form,extensions) { //v1.0
  343.   document.MM_returnValue = true;
  344.   if (extensions && extensions != '') {
  345.     for (var i = 0; i<form.elements.length; i++) {
  346.       field = form.elements[i];
  347.       if (field.type.toUpperCase() != 'FILE') continue;
  348.       if (field.value == '') {
  349.         alert('文件框中必须保证已经有文件被选中!');
  350.         document.MM_returnValue = false;field.focus();break;
  351.       }
  352.       if (extensions.toUpperCase().indexOf(getFileExtension(field.value).toUpperCase()) == -1) {
  353.         alert('这种文件类型不允许上传!.n只有以下类型的文件才被允许上传: ' + extensions + '.n请选择别的文件并重新上传.');
  354.         document.MM_returnValue = false;field.focus();break;
  355.   } } }
  356. }
  357. //-->
  358. </script>
  359. </head>
  360. <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
  361. <form action="<%=GP_uploadAction%>" method="post" enctype="multipart/form-data" name="form1" onSubmit="checkFileUpload(this,'GIF,JPG,JPEG,BMP,PNG');return document.MM_returnValue">
  362.   <table width="300" height="100" border="0" cellpadding="0" cellspacing="10">
  363.     <tr> 
  364.       <td align="center" valign="bottom" class="title"><strong>不能超过 1m</strong></td>
  365.     </tr>
  366.     <tr> 
  367.       <td align="center" valign="top"> 
  368.         <input name="file" type="file" size="20">
  369.         <input type="submit" name="Submit" value="提交">
  370.       </td>
  371.     </tr>
  372.   </table>
  373. </form>
  374. </body>
  375. </html>