0

I want to Insert record and upload file at the same time, right now im using FreeASPUpload Script. When i submit the form it returns this error

Cannot use the generic Request collection after calling BinaryRead

Here is the Full Source Code of my page

<% 
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
%>

I have been searching for this problem, but couldn't make it work. although what i found is that i have to use the Form Collection provided by FreeASPUpload. therefore i change

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

to this

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

But it also returns an error, that says: Variable is undefined: 'Upload'

If i change the Request method, it only Uploads the file not inserts the record

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

What i understands is that my insert query is in wrong place or so...

Please help me solve this problem.. thanks

yaqoob
  • 1,052
  • 3
  • 14
  • 39

1 Answers1

1

I haven't used AspFreeUpload much so I'm guessing a bit here.

It would appear that using the Request object isn't an option so you're stuck with having to use the Upload.Form. As your code stands, the Upload object is only defined and set within the context of your SaveFiles function.

Try moving your database insert code to within the SaveFiles function. This would mean cutting everything from the line

Dim objConn, objRs

to

InsCom.Execute

and pasting it just before 'End Function'

You may also need to move your include adovbs.inc directive to somewhere before the function was called. The most logical place would be on the line immediately below your other include directive = for uploadclass.asp

John
  • 4,658
  • 2
  • 14
  • 23
  • Thanks alot John, i have move all the code as you suggested. But there's one problem though with the `ADOVBS.INC` the variables that were defined in this file aren't recognized since i moved the file below `UploadClass.asp` but declaring them explicitly did the job done. So once again thank you so much, NOW my problem is solved :) – yaqoob Aug 17 '13 at 03:14
  • 1
    Glad you got there. I suppose you could also include adovbs.inc inside the function itself, but declaring the only variables you need is a neater solution IMO. I hardly ever use adovbs.inc – John Aug 17 '13 at 09:48
  • Couple of things **1.** Use `` [Using METADATA to Import DLL Constants](http://www.4guysfromrolla.com/webtech/110199-1.shtml) instead of `adovbs.asp` in your `global.asa` file to give all your ASP pages access to the ADO constants. **2.** The reason you can't use `Request.Form` is because once you call `Request.BinaryRead()` the Request collections are invalidated which is why AspFreeUpload provides it's own `Scripting.Dictionary` for accessing these values. – user692942 Mar 27 '14 at 15:59
  • @Lankymart - well, the OP was using adovbs.inc so I stuck with it. Personally I tend to use something like `rs.open sql,conn,0,1` so I don't need the constants – John Mar 27 '14 at 16:39
  • Since discovering METADATA Typelib tags I won't do it any other way. Personally I think from a some one looking at the code, that `rs.open sql, conn, adOpenForwardOnly, adLockReadOnly` is easier to read and understand the meaning, but each to their own. – user692942 Mar 27 '14 at 16:49