2013-08-16 57 views
0

我想同时插入记录和上传文件,现在我正在使用FreeASPUpload脚本。当我提交表单,它返回该错误如何使用FreeASPUpload脚本插入记录和上传文件

Cannot use the generic Request collection after calling BinaryRead 

这里是我的网页

<% 
option explicit 
Response.Expires = -1 
Server.ScriptTimeout = 600 
Session.CodePage = 65001 
%> 
<!-- #include file="UploadClass.asp" --> 
<% 

    Dim uploadsDirVar 
    uploadsDirVar = server.MapPath("Files_Uploaded") 

function OutputForm() 
%> 
<form name="frmSend" method="POST" enctype="multipart/form-data" accept-charset="utf-8" action="form.asp" onSubmit="return onSubmitForm();"> 
<input type="hidden" name="ApplicationForm" value="Insert" /> 
Name: <input type="text" name="name_insert" value="" size="30" /> 
    <B>File names:</B><br> 
    File 1: <input name="attach1" type="file" size=35><br> 
    <br> 
    <input style="margin-top:4" type="submit" value="Submit"> 
    </form> 
<% 
end function 

function TestEnvironment() 
    Dim fso, fileName, testFile, streamTest 
    TestEnvironment = "" 
    Set fso = Server.CreateObject("Scripting.FileSystemObject") 
    if not fso.FolderExists(uploadsDirVar) then 
     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." 
     exit function 
    end if 
    fileName = uploadsDirVar & "\test.txt" 
    on error resume next 
    Set testFile = fso.CreateTextFile(fileName, true) 
    If Err.Number<>0 then 
     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." 
     exit function 
    end if 
    Err.Clear 
    testFile.Close 
    fso.DeleteFile(fileName) 
    If Err.Number<>0 then 
     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." 
     exit function 
    end if 
    Err.Clear 
    Set streamTest = Server.CreateObject("ADODB.Stream") 
    If Err.Number<>0 then 
     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." 
     exit function 
    end if 
    Set streamTest = Nothing 
end function 

function SaveFiles 
    Dim Upload, fileName, fileSize, ks, i, fileKey 

    Set Upload = New FreeASPUpload 
    Upload.Save(uploadsDirVar) 

    ' If something fails inside the script, but the exception is handled 
    If Err.Number<>0 then Exit function 

    SaveFiles = "" 
    ks = Upload.UploadedFiles.keys 
    if (UBound(ks) <> -1) then 
     SaveFiles = "<B>Files uploaded:</B> " 
     for each fileKey in Upload.UploadedFiles.keys 
      SaveFiles = SaveFiles & Upload.UploadedFiles(fileKey).FileName & " (" & Upload.UploadedFiles(fileKey).Length & "B) " 
     next 
    else 
     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." 
    end if 
    SaveFiles = SaveFiles & "<br>Enter a number = " & Upload.Form("enter_a_number") & "<br>" 
    SaveFiles = SaveFiles & "Checkbox values = " & Upload.Form("checkbox_values") & "<br>" 
    SaveFiles = SaveFiles & "List values = " & Upload.Form("list_values") & "<br>" 
    SaveFiles = SaveFiles & "Text area = " & Upload.Form("t_area") & "<br>" 
end function 
%> 
<HTML> 
<HEAD> 
<TITLE>Test Free ASP Upload 2.0</TITLE> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<style> 
BODY {background-color: white;font-family:arial; font-size:12} 
</style> 
<script> 
function onSubmitForm() { 
    var formDOMObj = document.frmSend; 
    if (formDOMObj.attach1.value == "") 
     alert("Please press the Browse button and pick a file.") 
    else 
     return true; 
    return false; 
} 
</script> 

</HEAD> 

<BODY> 

<br><br> 
<div style="border-bottom: #A91905 2px solid;font-size:16">Upload files to your server</div> 
<% 
Dim diagnostics 
if Request.ServerVariables("REQUEST_METHOD") <> "POST" then 
    diagnostics = TestEnvironment() 
    if diagnostics<>"" then 
     response.write "<div style=""margin-left:20; margin-top:30; margin-right:30; margin-bottom:30;"">" 
     response.write diagnostics 
     response.write "<p>After you correct this problem, reload the page." 
     response.write "</div>" 
    else 
     response.write "<div style=""margin-left:150"">" 
     OutputForm() 
     response.write "</div>" 
    end if 
else 
    response.write "<div style=""margin-left:150"">" 
    OutputForm() 
    response.write SaveFiles() 
    response.write "<br><br></div>" 
end if 

%> 

</BODY> 
</HTML> 
<!-- #include file="ADOVBS.inc" --> 
<% 

'======================================================================================= 
' CONNECT DATABASE 
'======================================================================================= 
Dim objConn, objRs 
Set objConn = CreateObject("ADODB.Connection") 
Set objRs = CreateObject("ADODB.Recordset") 
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" 

If Request("ApplicationForm") = "Insert" Then 
Set InsCom=Server.CreateObject("ADODB.Command") 
InsCom.ActiveConnection=objConn 

InsName = Trim(request("name_insert")) 
InsName = replace(InsName,"'","''") 

InsCom.CommandText = "Insert into applications(aname)Values(?)" 
InsCom.Parameters.Append InsCom.CreateParameter("@name_insert", adVarChar, adParamInput, 255, InsName) 

InsCom.Execute 

End If 
%> 

我一直在寻找这个问题的完整的源代码,但不能使它工作。尽管我发现我必须使用FreeASPUpload提供的Form Collection。因此我改变

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

这个

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

但它也返回一个错误,即说:变量未定义:“上传”

如果我改变请求方法,它只上传文件不插入记录

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

什么我的理解是,我的插入查询是在错误的地方左右...

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

回答

1

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

看来使用Request对象不是一个选项,所以你不得不使用Upload.Form。如您的代码所示,Upload对象仅在SaveFiles函数的上下文中定义和设置。

尝试将数据库插入代码移到SaveFiles函数中。这意味着从线切割一切

Dim objConn, objRs 

InsCom.Execute 

和之前“端功能”

您可能还需要移动你的粘贴包括ADOVBS.INC指令某处在函数被调用之前。最合乎逻辑的地方就在你的其他包含指令下面的行上= uploadclass.asp

+0

非常感谢John,我已经按照你的建议移动了所有的代码。但是有一个问题,虽然'ADOVBS.INC'在这个文件中定义的变量没有被识别,因为我把文件移动到'UploadClass.asp'下面,但是明确地声明它们完成了工作。所以再次非常感谢你,现在我的问题解决了:) – yaqoob

+1

很高兴你到了那里。我想你也可以在函数本身中包含adovbs.inc,但声明你需要的唯一变量是一个整洁的解决方案IMO。我很少使用adovbs.inc – John

+0

几件事** 1。**使用'<! - METADATA TYPE =“typelib”FILE =“C:\ Program Files \ Common Files \ System \ ADO \ msado20.tlb” - - >'[使用METADATA导入DLL常量](http://www.4guysfromrolla.com/webtech/110199-1.shtml)而不是'global.asa'文件中的'adovbs.asp'来给出所有的ASP页面访问ADO常量。 ** 2。**您无法使用Request.Form的原因是因为您一旦调用Request,BinaryRead()'请求集合被无效,这就是为什么AspFreeUpload提供它自己的'Scripting.Dictionary'来访问这些值的原因。 – Lankymart

相关问题