upload_json.asp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <%@ CODEPAGE=65001 %>
  2. <% Option Explicit %>
  3. <% Response.CodePage=65001 %>
  4. <% Response.Charset="UTF-8" %>
  5. <!--#include file="UpLoad_Class.asp"-->
  6. <!--#include file="JSON_2.0.4.asp"-->
  7. <%
  8. ' KindEditor ASP
  9. '
  10. ' 本ASP程序是演示程序,建议不要直接在实际项目中使用。
  11. ' 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。
  12. '
  13. Dim aspUrl, savePath, saveUrl, maxSize, fileName, fileExt, newFileName, filePath, fileUrl, dirName
  14. Dim extStr, imageExtStr, flashExtStr, mediaExtStr, fileExtStr
  15. Dim upload, file, fso, ranNum, hash, ymd, mm, dd, result
  16. aspUrl = Request.ServerVariables("SCRIPT_NAME")
  17. aspUrl = left(aspUrl, InStrRev(aspUrl, "/"))
  18. '文件保存目录路径
  19. savePath = "../attached/"
  20. '文件保存目录URL
  21. saveUrl = aspUrl & "../attached/"
  22. '定义允许上传的文件扩展名
  23. imageExtStr = "gif|jpg|jpeg|png|bmp"
  24. flashExtStr = "swf|flv"
  25. mediaExtStr = "swf|flv|mp3|wav|wma|wmv|mid|avi|mpg|asf|rm|rmvb"
  26. fileExtStr = "doc|docx|xls|xlsx|ppt|htm|html|txt|zip|rar|gz|bz2"
  27. '最大文件大小
  28. maxSize = 5 * 1024 * 1024 '5M
  29. Set fso = Server.CreateObject("Scripting.FileSystemObject")
  30. If Not fso.FolderExists(Server.mappath(savePath)) Then
  31. showError("上传目录不存在。")
  32. End If
  33. dirName = Request.QueryString("dir")
  34. If isEmpty(dirName) Then
  35. dirName = "image"
  36. End If
  37. If instr(lcase("image,flash,media,file"), dirName) < 1 Then
  38. showError("目录名不正确。")
  39. End If
  40. Select Case dirName
  41. Case "flash" extStr = flashExtStr
  42. Case "media" extStr = mediaExtStr
  43. Case "file" extStr = fileExtStr
  44. Case Else extStr = imageExtStr
  45. End Select
  46. set upload = new AnUpLoad
  47. upload.Exe = extStr
  48. upload.MaxSize = maxSize
  49. upload.GetData()
  50. if upload.ErrorID>0 then
  51. showError(upload.Description)
  52. end if
  53. '创建文件夹
  54. savePath = savePath & dirName & "/"
  55. saveUrl = saveUrl & dirName & "/"
  56. If Not fso.FolderExists(Server.mappath(savePath)) Then
  57. fso.CreateFolder(Server.mappath(savePath))
  58. End If
  59. mm = month(now)
  60. If mm < 10 Then
  61. mm = "0" & mm
  62. End If
  63. dd = day(now)
  64. If dd < 10 Then
  65. dd = "0" & dd
  66. End If
  67. ymd = year(now) & mm & dd
  68. savePath = savePath & ymd & "/"
  69. saveUrl = saveUrl & ymd & "/"
  70. If Not fso.FolderExists(Server.mappath(savePath)) Then
  71. fso.CreateFolder(Server.mappath(savePath))
  72. End If
  73. set file = upload.files("imgFile")
  74. if file is nothing then
  75. showError("请选择文件。")
  76. end if
  77. set result = file.saveToFile(savePath, 0, true)
  78. if result.error then
  79. showError(file.Exception)
  80. end if
  81. filePath = Server.mappath(savePath & file.filename)
  82. fileUrl = saveUrl & file.filename
  83. Set upload = nothing
  84. Set file = nothing
  85. If Not fso.FileExists(filePath) Then
  86. showError("上传文件失败。")
  87. End If
  88. Response.AddHeader "Content-Type", "text/html; charset=UTF-8"
  89. Set hash = jsObject()
  90. hash("error") = 0
  91. hash("url") = fileUrl
  92. hash.Flush
  93. Response.End
  94. Function showError(message)
  95. Response.AddHeader "Content-Type", "text/html; charset=UTF-8"
  96. Dim hash
  97. Set hash = jsObject()
  98. hash("error") = 1
  99. hash("message") = message
  100. hash.Flush
  101. Response.End
  102. End Function
  103. %>