表单 – 如何使用FreeASPUpload脚本插入记录和上载文件

前端之家收集整理的这篇文章主要介绍了表单 – 如何使用FreeASPUpload脚本插入记录和上载文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想同时插入记录和上传文件,现在我正在使用 FreeASPUpload脚本.当我提交表单时,它返回此错误
  1. Cannot use the generic Request collection after calling BinaryRead

这是我的页面的完整源代码

  1. <%
  2. option explicit
  3. Response.Expires = -1
  4. Server.ScriptTimeout = 600
  5. Session.CodePage = 65001
  6. %>
  7. <!-- #include file="UploadClass.asp" -->
  8. <%
  9.  
  10. Dim uploadsDirVar
  11. uploadsDirVar = server.MapPath("Files_Uploaded")
  12.  
  13. function OutputForm()
  14. %>
  15. <form name="frmSend" method="POST" enctype="multipart/form-data" accept-charset="utf-8" action="form.asp" onSubmit="return onSubmitForm();">
  16. <input type="hidden" name="ApplicationForm" value="Insert" />
  17. Name: <input type="text" name="name_insert" value="" size="30" />
  18. <B>File names:</B><br>
  19. File 1: <input name="attach1" type="file" size=35><br>
  20. <br>
  21. <input style="margin-top:4" type="submit" value="Submit">
  22. </form>
  23. <%
  24. end function
  25.  
  26. function TestEnvironment()
  27. Dim fso,fileName,testFile,streamTest
  28. TestEnvironment = ""
  29. Set fso = Server.CreateObject("Scripting.FileSystemObject")
  30. if not fso.FolderExists(uploadsDirVar) then
  31. TestEnvironment = "<B>Folder " & uploadsDirVar & " does not exist.</B><br>The value of your uploadsDirVar is incorrect. Open uploadTester.asp in an editor and change the value of uploadsDirVar to the pathname of a directory with write permissions."
  32. exit function
  33. end if
  34. fileName = uploadsDirVar & "\test.txt"
  35. on error resume next
  36. Set testFile = fso.CreateTextFile(fileName,true)
  37. If Err.Number<>0 then
  38. TestEnvironment = "<B>Folder " & uploadsDirVar & " does not have write permissions.</B><br>The value of your uploadsDirVar is incorrect. Open uploadTester.asp in an editor and change the value of uploadsDirVar to the pathname of a directory with write permissions."
  39. exit function
  40. end if
  41. Err.Clear
  42. testFile.Close
  43. fso.DeleteFile(fileName)
  44. If Err.Number<>0 then
  45. TestEnvironment = "<B>Folder " & uploadsDirVar & " does not have delete permissions</B>,although it does have write permissions.<br>Change the permissions for IUSR_<I>computername</I> on this folder."
  46. exit function
  47. end if
  48. Err.Clear
  49. Set streamTest = Server.CreateObject("ADODB.Stream")
  50. If Err.Number<>0 then
  51. TestEnvironment = "<B>The ADODB object <I>Stream</I> is not available in your server.</B><br>Check the Requirements page for information about upgrading your ADODB libraries."
  52. exit function
  53. end if
  54. Set streamTest = Nothing
  55. end function
  56.  
  57. function SaveFiles
  58. Dim Upload,fileSize,ks,i,fileKey
  59.  
  60. Set Upload = New FreeASPUpload
  61. Upload.Save(uploadsDirVar)
  62.  
  63. ' If something fails inside the script,but the exception is handled
  64. If Err.Number<>0 then Exit function
  65.  
  66. SaveFiles = ""
  67. ks = Upload.UploadedFiles.keys
  68. if (UBound(ks) <> -1) then
  69. SaveFiles = "<B>Files uploaded:</B> "
  70. for each fileKey in Upload.UploadedFiles.keys
  71. SaveFiles = SaveFiles & Upload.UploadedFiles(fileKey).FileName & " (" & Upload.UploadedFiles(fileKey).Length & "B) "
  72. next
  73. else
  74. SaveFiles = "No file selected for upload or the file name specified in the upload form does not correspond to a valid file in the system."
  75. end if
  76. SaveFiles = SaveFiles & "<br>Enter a number = " & Upload.Form("enter_a_number") & "<br>"
  77. SaveFiles = SaveFiles & "CheckBox values = " & Upload.Form("checkBox_values") & "<br>"
  78. SaveFiles = SaveFiles & "List values = " & Upload.Form("list_values") & "<br>"
  79. SaveFiles = SaveFiles & "Text area = " & Upload.Form("t_area") & "<br>"
  80. end function
  81. %>
  82. <HTML>
  83. <HEAD>
  84. <TITLE>Test Free ASP Upload 2.0</TITLE>
  85. <Meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  86. <style>
  87. BODY {background-color: white;font-family:arial; font-size:12}
  88. </style>
  89. <script>
  90. function onSubmitForm() {
  91. var formDOMObj = document.frmSend;
  92. if (formDOMObj.attach1.value == "")
  93. alert("Please press the Browse button and pick a file.")
  94. else
  95. return true;
  96. return false;
  97. }
  98. </script>
  99.  
  100. </HEAD>
  101.  
  102. <BODY>
  103.  
  104. <br><br>
  105. <div style="border-bottom: #A91905 2px solid;font-size:16">Upload files to your server</div>
  106. <%
  107. Dim diagnostics
  108. if Request.ServerVariables("REQUEST_METHOD") <> "POST" then
  109. diagnostics = TestEnvironment()
  110. if diagnostics<>"" then
  111. response.write "<div style=""margin-left:20; margin-top:30; margin-right:30; margin-bottom:30;"">"
  112. response.write diagnostics
  113. response.write "<p>After you correct this problem,reload the page."
  114. response.write "</div>"
  115. else
  116. response.write "<div style=""margin-left:150"">"
  117. OutputForm()
  118. response.write "</div>"
  119. end if
  120. else
  121. response.write "<div style=""margin-left:150"">"
  122. OutputForm()
  123. response.write SaveFiles()
  124. response.write "<br><br></div>"
  125. end if
  126.  
  127. %>
  128.  
  129. </BODY>
  130. </HTML>
  131. <!-- #include file="ADOVBS.inc" -->
  132. <%
  133.  
  134. '=======================================================================================
  135. ' CONNECT DATABASE
  136. '=======================================================================================
  137. Dim objConn,objRs
  138. Set objConn = CreateObject("ADODB.Connection")
  139. Set objRs = CreateObject("ADODB.Recordset")
  140. objConn.open"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& server.MapPath("db/Job_database.mdb") &";Mode=ReadWrite|Share Deny None;Persist Security Info=False"
  141.  
  142. If Request("ApplicationForm") = "Insert" Then
  143. Set InsCom=Server.CreateObject("ADODB.Command")
  144. InsCom.ActiveConnection=objConn
  145.  
  146. InsName = Trim(request("name_insert"))
  147. InsName = replace(InsName,"'","''")
  148.  
  149. InsCom.CommandText = "Insert into applications(aname)Values(?)"
  150. InsCom.Parameters.Append InsCom.CreateParameter("@name_insert",adVarChar,adParamInput,255,InsName)
  151.  
  152. InsCom.Execute
  153.  
  154. End If
  155. %>

我一直在寻找这个问题,但无法使其发挥作用.虽然我发现我必须使用FreeASPUpload提供的Form Collection.因此,我改变了

  1. If Request("ApplicationForm") = "Insert" Then

对此

  1. If Upload.Form("ApplicationForm") = "Insert" Then

但它也会返回一个错误,即:变量未定义:’上传

如果我更改了Request方法,则只上传文件而不是插入记录

  1. If Request.QueryString("ApplicationForm") = "Insert" Then

我理解的是我的插入查询错误的位置左右…

请帮我解决这个问题..谢谢

解决方法

我没有太多使用AspFreeUpload所以我在这里猜测一下.

似乎使用Request对象不是一个选项,所以你不得不使用Upload.Form.正如您的代码所代表的那样,Upload对象仅在SaveFiles函数的上下文中定义和设置.

尝试将数据库插入代码移动到SaveFiles函数中.这意味着从生产线切割所有东西

  1. Dim objConn,objRs

  1. InsCom.Execute

并在“结束功能”之前粘贴它

调用函数之前,您可能还需要将include adovbs.inc指令移动到某处.最合乎逻辑的地方就是在你的另一个include指令= for uploadclass.asp的正下方

猜你在找的HTML相关文章