Upload.asp
资源名称:1.rar [点击查看]
上传用户:yrf020
上传日期:2007-07-24
资源大小:1287k
文件大小:11k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

HTML/CSS

  1. <!--#include file="Include/Startup.asp"-->
  2. <!--#include file="Include/upfile_class.asp"-->
  3. <%
  4. Server.ScriptTimeOut = 1800
  5. ' 参数变量
  6. Dim sType, sStyleName
  7. ' 设置变量
  8. Dim sAllowExt, nAllowSize, sUploadDir, nUploadObject, nAutoDir, sBaseUrl, sContentPath
  9. ' 接口变量
  10. Dim sFileExt, sOriginalFileName, sSaveFileName, sPathFileName, nFileNum
  11. Call DBConnBegin() ' 初始化数据库连接
  12. Call InitUpload() ' 初始化上传变量
  13. Call DBConnEnd() ' 断开数据库连接
  14. Dim sAction
  15. sAction = UCase(Trim(Request.QueryString("action")))
  16. Select Case sAction
  17. Case "REMOTE"
  18. Call DoRemote() ' 远程自动获取
  19. Case "SAVE"
  20. Call ShowForm() ' 显示上传表单
  21. Call DoSave() ' 存文件
  22. Case Else
  23. Call ShowForm() ' 显示上传表单
  24. End Select
  25. Sub ShowForm() 
  26. %>
  27. <HTML>
  28. <HEAD>
  29. <TITLE>文件上传</TITLE>
  30. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  31. <style type="text/css">
  32. body, a, table, div, span, td, th, input, select{font:9pt;font-family: "宋体", Verdana, Arial, Helvetica, sans-serif;}
  33. body {padding:0px;margin:0px}
  34. </style>
  35. <script language="JavaScript" src="dialog/dialog.js"></script>
  36. </head>
  37. <body bgcolor=menu>
  38. <form action="?action=save&type=<%=sType%>&style=<%=sStyleName%>" method=post name=myform enctype="multipart/form-data">
  39. <input type=file name=uploadfile size=1 style="width:100%" onChange="originalfile.value=this.value">
  40. <input type=hidden name=originalfile value="">
  41. </form>
  42. <script language=javascript>
  43. var sAllowExt = "<%=sAllowExt%>";
  44. // 检测上传表单
  45. function CheckUploadForm() {
  46. if (!IsExt(document.myform.uploadfile.value,sAllowExt)){
  47. parent.UploadError("提示:nn请选择一个有效的文件,n支持的格式有("+sAllowExt+")!");
  48. return false;
  49. }
  50. return true
  51. }
  52. // 提交事件加入检测表单
  53. var oForm = document.myform ;
  54. oForm.attachEvent("onsubmit", CheckUploadForm) ;
  55. if (! oForm.submitUpload) oForm.submitUpload = new Array() ;
  56. oForm.submitUpload[oForm.submitUpload.length] = CheckUploadForm ;
  57. if (! oForm.originalSubmit) {
  58. oForm.originalSubmit = oForm.submit ;
  59. oForm.submit = function() {
  60. if (this.submitUpload) {
  61. for (var i = 0 ; i < this.submitUpload.length ; i++) {
  62. this.submitUpload[i]() ;
  63. }
  64. }
  65. this.originalSubmit() ;
  66. }
  67. }
  68. // 上传表单已装入完成
  69. try {
  70. parent.UploadLoaded();
  71. }
  72. catch(e){
  73. }
  74. </script>
  75. </body>
  76. </html>
  77. <% 
  78. End Sub 
  79. ' 保存操作
  80. Sub DoSave()
  81. ' 默认无组件上传类
  82. Call DoUpload_Class
  83. sPathFileName = sContentPath & sSaveFileName
  84. Call OutScript("parent.UploadSaved('" & sPathFileName & "');var obj=parent.dialogArguments.dialogArguments;if (!obj) obj=parent.dialogArguments;try{obj.addUploadFile('" & sOriginalFileName & "', '" & sSaveFileName & "', '" & sPathFileName & "');} catch(e){}")
  85. End Sub
  86. ' 自动获取远程文件
  87. Sub DoRemote()
  88. Dim sContent, i
  89. For i = 1 To Request.Form("eWebEditor_UploadText").Count 
  90. sContent = sContent & Request.Form("eWebEditor_UploadText")(i) 
  91. Next
  92. If sAllowExt <> "" Then
  93. sContent = ReplaceRemoteUrl(sContent, sAllowExt)
  94. End If
  95. Response.Write "<HTML><HEAD><TITLE>远程上传</TITLE><meta http-equiv='Content-Type' content='text/html; charset=gb2312'></head><body>" & _
  96. "<input type=hidden id=UploadText value=""" & inHTML(sContent) & """>" & _
  97. "</body></html>"
  98. Call OutScriptNoBack("parent.setHTML(UploadText.value);try{parent.addUploadFile('" & sOriginalFileName & "', '" & sSaveFileName & "', '" & sPathFileName & "');} catch(e){} parent.remoteUploadOK();")
  99. End Sub
  100. ' 无组上传类
  101. Sub DoUpload_Class()
  102. On Error Resume Next
  103. Dim oUpload, oFile
  104. ' 建立上传对象
  105. Set oUpload = New upfile_class
  106. ' 取得上传数据,限制最大上传
  107. oUpload.GetData(nAllowSize*1024)
  108. If oUpload.Err > 0 Then
  109. Select Case oUpload.Err
  110. Case 1
  111. Call OutScript("parent.UploadError('请选择有效的上传文件!')")
  112. Case 2
  113. Call OutScript("parent.UploadError('你上传的文件总大小超出了最大限制(" & nAllowSize & "KB)!')")
  114. End Select
  115. Response.End
  116. End If
  117. Set oFile = oUpload.File("uploadfile")
  118. sFileExt = LCase(oFile.FileExt)
  119. Call CheckValidExt(sFileExt)
  120. sOriginalFileName = oFile.FileName
  121. sSaveFileName = GetRndFileName(sFileExt)
  122. oFile.SaveToFile Server.Mappath(sUploadDir & sSaveFileName)
  123. Set oFile = Nothing
  124. Set oUpload = Nothing
  125. End Sub
  126. ' 取随机文件名
  127. Function GetRndFileName(sExt)
  128. Dim sRnd
  129. Randomize
  130. sRnd = Int(900 * Rnd) + 100
  131. GetRndFileName = year(now) & month(now) & day(now) & hour(now) & minute(now) & second(now) & sRnd & "." & sExt
  132. End Function
  133. ' 输出客户端脚本
  134. Sub OutScript(str)
  135. Response.Write "<script language=javascript>" & str & ";history.back()</script>"
  136. End Sub
  137. Sub OutScriptNoBack(str)
  138. Response.Write "<script language=javascript>" & str & "</script>"
  139. End Sub
  140. ' 检测扩展名的有效性
  141. Sub CheckValidExt(sExt)
  142. Dim b, i, aExt
  143. b = False
  144. aExt = Split(sAllowExt, "|")
  145. For i = 0 To UBound(aExt)
  146. If LCase(aExt(i)) = sExt Then
  147. b = True
  148. Exit For
  149. End If
  150. Next
  151. If b = False Then
  152. OutScript("parent.UploadError('提示:nn请选择一个有效的文件,n支持的格式有("+sAllowExt+")!')")
  153. Response.End
  154. End If
  155. End Sub
  156. ' 初始化上传限制数据
  157. Sub InitUpload()
  158. sType = UCase(Trim(Request.QueryString("type")))
  159. sStyleName = Get_SafeStr(Trim(Request.QueryString("style")))
  160. sSql = "select * from ewebeditor_style where s_name='" & sStyleName & "'"
  161. oRs.Open sSql, oConn, 0, 1
  162. If Not oRs.Eof Then
  163. sBaseUrl = oRs("S_BaseUrl")
  164. nUploadObject = oRs("S_UploadObject")
  165. nAutoDir = oRs("S_AutoDir")
  166. sUploadDir = oRs("S_UploadDir")
  167. Select Case sBaseUrl
  168. Case "0"
  169. sContentPath = oRs("S_ContentPath")
  170. Case "1"
  171. sContentPath = RelativePath2RootPath(sUploadDir)
  172. Case "2"
  173. sContentPath = RootPath2DomainPath(RelativePath2RootPath(sUploadDir))
  174. End Select
  175. Select Case sType
  176. Case "REMOTE"
  177. sAllowExt = oRs("S_RemoteExt")
  178. nAllowSize = oRs("S_RemoteSize")
  179. Case "FILE"
  180. sAllowExt = oRs("S_FileExt")
  181. nAllowSize = oRs("S_FileSize")
  182. Case "MEDIA"
  183. sAllowExt = oRs("S_MediaExt")
  184. nAllowSize = oRs("S_MediaSize")
  185. Case "FLASH"
  186. sAllowExt = oRs("S_FlashExt")
  187. nAllowSize = oRs("S_FlashSize")
  188. Case Else
  189. sAllowExt = oRs("S_ImageExt")
  190. nAllowSize = oRs("S_ImageSize")
  191. End Select
  192. Else
  193. OutScript("parent.UploadError('无效的样式ID号,请通过页面上的链接进行操作!')")
  194. End If
  195. oRs.Close
  196. ' 任何情况下都不允许上传asp脚本文件
  197. sAllowExt = Replace(UCase(sAllowExt), "ASP", "")
  198. End Sub
  199. ' 转为根路径格式
  200. Function RelativePath2RootPath(url)
  201. Dim sTempUrl
  202. sTempUrl = url
  203. If Left(sTempUrl, 1) = "/" Then
  204. RelativePath2RootPath = sTempUrl
  205. Exit Function
  206. End If
  207. Dim sWebEditorPath
  208. sWebEditorPath = Request.ServerVariables("SCRIPT_NAME")
  209. sWebEditorPath = Left(sWebEditorPath, InstrRev(sWebEditorPath, "/") - 1)
  210. Do While Left(sTempUrl, 3) = "../"
  211. sTempUrl = Mid(sTempUrl, 4)
  212. sWebEditorPath = Left(sWebEditorPath, InstrRev(sWebEditorPath, "/") - 1)
  213. Loop
  214. RelativePath2RootPath = sWebEditorPath & "/" & sTempUrl
  215. End Function
  216. ' 根路径转为带域名全路径格式
  217. Function RootPath2DomainPath(url)
  218. Dim sHost, sPort
  219. sHost = Split(Request.ServerVariables("SERVER_PROTOCOL"), "/")(0) & "://" & Request.ServerVariables("HTTP_HOST")
  220. sPort = Request.ServerVariables("SERVER_PORT")
  221. If sPort <> "80" Then
  222. sHost = sHost & ":" & sPort
  223. End If
  224. RootPath2DomainPath = sHost & url
  225. End Function
  226. '================================================
  227. '作  用:替换字符串中的远程文件为本地文件并保存远程文件
  228. '参  数:
  229. ' sHTML : 要替换的字符串
  230. ' sExt : 执行替换的扩展名
  231. '================================================
  232. Function ReplaceRemoteUrl(sHTML, sExt)
  233. Dim s_Content
  234. s_Content = sHTML
  235. If IsObjInstalled("Microsoft.XMLHTTP") = False then
  236. ReplaceRemoteUrl = s_Content
  237. Exit Function
  238. End If
  239. Dim re, RemoteFile, RemoteFileurl, SaveFileName, SaveFileType
  240. Set re = new RegExp
  241. re.IgnoreCase  = True
  242. re.Global = True
  243. re.Pattern = "((http|https|ftp|rtsp|mms):(//|\\){1}(([A-Za-z0-9_-])+[.]){1,}(net|com|cn|org|cc|tv|[0-9]{1,3})(S*/)((S)+[.]{1}(" & sExt & ")))"
  244. Set RemoteFile = re.Execute(s_Content)
  245. Dim a_RemoteUrl(), n, i, bRepeat
  246. n = 0
  247. ' 转入无重复数据
  248. For Each RemoteFileurl in RemoteFile
  249. If n = 0 Then
  250. n = n + 1
  251. Redim a_RemoteUrl(n)
  252. a_RemoteUrl(n) = RemoteFileurl
  253. Else
  254. bRepeat = False
  255. For i = 1 To UBound(a_RemoteUrl)
  256. If UCase(RemoteFileurl) = UCase(a_RemoteUrl(i)) Then
  257. bRepeat = True
  258. Exit For
  259. End If
  260. Next
  261. If bRepeat = False Then
  262. n = n + 1
  263. Redim Preserve a_RemoteUrl(n)
  264. a_RemoteUrl(n) = RemoteFileurl
  265. End If
  266. End If
  267. Next
  268. ' 开始替换操作
  269. nFileNum = 0
  270. For i = 1 To n
  271. SaveFileType = Mid(a_RemoteUrl(i), InstrRev(a_RemoteUrl(i), ".") + 1)
  272. SaveFileName = GetRndFileName(SaveFileType)
  273. If SaveRemoteFile(SaveFileName, a_RemoteUrl(i)) = True Then
  274. nFileNum = nFileNum + 1
  275. If nFileNum > 0 Then
  276. sOriginalFileName = sOriginalFileName & "|"
  277. sSaveFileName = sSaveFileName & "|"
  278. sPathFileName = sPathFileName & "|"
  279. End If
  280. sOriginalFileName = sOriginalFileName & Mid(a_RemoteUrl(i), InstrRev(a_RemoteUrl(i), "/") + 1)
  281. sSaveFileName = sSaveFileName & SaveFileName
  282. sPathFileName = sPathFileName & sContentPath & SaveFileName
  283. s_Content = Replace(s_Content, a_RemoteUrl(i), sContentPath & SaveFileName, 1, -1, 1)
  284. End If
  285. Next
  286. ReplaceRemoteUrl = s_Content
  287. End Function
  288. '================================================
  289. '作  用:保存远程的文件到本地
  290. '参  数:s_LocalFileName ------ 本地文件名
  291. '  s_RemoteFileUrl ------ 远程文件URL
  292. '返回值:True  ----成功
  293. '        False ----失败
  294. '================================================
  295. Function SaveRemoteFile(s_LocalFileName, s_RemoteFileUrl)
  296. Dim Ads, Retrieval, GetRemoteData
  297. Dim bError
  298. bError = False
  299. SaveRemoteFile = False
  300. On Error Resume Next
  301. Set Retrieval = Server.CreateObject("Microsoft.XMLHTTP")
  302. With Retrieval
  303. .Open "Get", s_RemoteFileUrl, False, "", ""
  304. .Send
  305. GetRemoteData = .ResponseBody
  306. End With
  307. Set Retrieval = Nothing
  308. If LenB(GetRemoteData) > nAllowSize*1024 Then
  309. bError = True
  310. Else
  311. Set Ads = Server.CreateObject("Adodb.Stream")
  312. With Ads
  313. .Type = 1
  314. .Open
  315. .Write GetRemoteData
  316. .SaveToFile Server.MapPath(sUploadDir & s_LocalFileName), 2
  317. .Cancel()
  318. .Close()
  319. End With
  320. Set Ads=nothing
  321. End If
  322. If Err.Number = 0 And bError = False Then
  323. SaveRemoteFile = True
  324. Else
  325. Err.Clear
  326. End If
  327. End Function
  328. '================================================
  329. '作  用:检查组件是否已经安装
  330. '参  数:strClassString ----组件名
  331. '返回值:True  ----已经安装
  332. '        False ----没有安装
  333. '================================================
  334. Function IsObjInstalled(strClassString)
  335. On Error Resume Next
  336. IsObjInstalled = False
  337. Err = 0
  338. Dim xTestObj
  339. Set xTestObj = Server.CreateObject(strClassString)
  340. If 0 = Err Then IsObjInstalled = True
  341. Set xTestObj = Nothing
  342. Err = 0
  343. End Function
  344. %>