Add Record to Database and Upload an Image at the same time (2nd Attempt with different code)
Asked Answered
P

0

0

Having problems! I have messed with this code over and over. This is just one of my attempts. Trying to add a record to a database and upload an image at the same time. The first one I sent would add the record but, would not upload the image. This one will upload the image but, will not add the record. My patience is running out. On this one I'm getting a error saying I can't use generic requests.

<%@ Language=VBScript %>
<% 
option explicit 
Response.Expires = -1
Server.ScriptTimeout = 600
Session.CodePage  = 65001

dim uid,thisuid,bizid,sourceid,email,PTitle,uname,conn,ucomments,RelatedTo,ToMessage,imgid,sql
uid = "JIU645OIuoiUI6435OIUhouihoHI"
%>
<!-- #include file="freeaspupload.asp" -->
<%
Dim uploadsDirVar
uploadsDirVar = "c:\inetpub\wwwroot\the-website\users\" & uid & "\"

function OutputForm()
%>
 <div class="w3-card w3-round w3-white">
   <div class="w3-container">
     <h6 class="w3-opacity">Create A Post</h6>
       <form method="POST" enctype="multipart/form-data" accept-charset="utf-8" action="uploadTester.asp" onSubmit="return onSubmitForm();">
         <input name="UComments" type="text" style="padding:5px;width:100%;" placeholder="Type your comments..." required>
         <br><br>
          &nbsp;<button type="submit" class="w3-btn w3-theme" value="Upload"><i class="fa fa-pencil"></i>&nbsp;Post</button>
         <div class="photodiv w3-btn w3-theme"><i class="fa fa-image"></i>&nbsp;Photo<input type="file" name="attach1" class="hide_file"></button></div>
         <input hidden name="sourceid" value="<%= sourceid %>">
         <input hidden name="uid" value="<%= uid %>">
         <input type="hidden" name="NewMess" size="20" value="Yes">
         <input type="hidden" name="RelatedTo" size="20" value="0">
         <input type="hidden" name="Email" value="<%= email %>">
         <input type="hidden" name="TableName" value="ProWall">
         <input type="hidden" name="Title" value="<%= PTitle %>">
         <input type="hidden" name="ThisMessage" size="20" value="0">
         <input type="hidden" name="Name" value="<%= uname %>">
       </form>
       </div><div>&nbsp;</div>
     </div>
<%
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, filedname

    Set Upload = New FreeASPUpload
    Upload.Save(uploadsDirVar)

    filedname = 1
    for each fileKey in Upload.UploadedFiles.keys
      if filedname = 1 then
        filedname = filedname + 1
        imgid = Upload.UploadedFiles(fileKey).FileName
      end if
    next

    uid=Upload.Form("uid")
    thisuid=Upload.Form("thisuid")
    email=Upload.Form("email")
    ucomments=Upload.Form("ucomments")
    RelatedTo=Upload.Form("RelatedTo")
    ToMessage=Upload.Form("ToMessage")

    if ToMessage = "" then
      ToMessage = 0
    end if

    if RelatedTo = "" then
      RelatedTo = 0
    End if

    set conn=Server.CreateObject("ADODB.Connection")
    conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; DATA SOURCE=c:/inetpub/wwwroot/the-website/contents/page/MBoard.mdb"
    sql="INSERT INTO ProWall (uid,sourceid,email,ucomments,posted,RelatedTo,ToMessage,imgid) VALUES ('"&uid&"','"&thisuid&"','"&email&"','"&ucomments&"','"&Now&"','"&RelatedTo&"','"&ToMessage&"','"&imgid&"';"
    Set rs= Server.CreateObject("ADODB.Recordset")
    rs.Open sql, conn, 3, 2
    conn.close
    set conn = nothing

'response.redirect "show-msg.asp"

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

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 == "" && formDOMObj.attach2.value == "" && formDOMObj.attach3.value == "" && formDOMObj.attach4.value == "" )
    alert("Please press the Browse button and pick a file.")
    else
        return true;
    return false;
}
</script>

</HEAD>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://www.w3schools.com/lib/w3-theme-blue-grey.css">
<link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Open+Sans'>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
html, body, h1, h2, h3, h4, h5 {font-family: Open Sans, sans-serif;}

.photodiv{
  padding:8px 16px;
  background:;
  border:0px;
  position:relative;
  color:#fff;
  border-radius:2px;
  text-align:center;
  float:left;
  cursor:pointer
}
.hide_file {
    position: absolute;
    z-index: 1000;
    opacity: 0;
    cursor: pointer;
    right: 0;
    top: 0;
    height: 100%;
    font-size: 24px;
    width: 100%;

}
</style>
<BODY onload="OutputForm()">

<%
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>
Poised answered 5/8, 2019 at 3:28 Comment(8)
I tried changing request() to request.form() and got te following error - Cannot use Request.Form collection after calling BinaryRead.Poised
Ok.. I have taken the request.form() out of the sql statement and moved them above the file upload. The image is still uploading but, IIS seems to get stuck. So I took the .form part out now it still won't add the record but, does not cause a error. File still uploads. Round and round I go.Poised
It seems to be working now. I had left out a piece that I thought I didn't need. Put it back in and... Ummm, still testing to make sure. I might have made a breakthroughPoised
Well, thought I had it but, it's still not passing the inputs through. I had it hard coded and it uploaded the image and posted data to the database like it should. I took out the written code and the request() still doesn't work. Yet, I get no errors. Somebody, Please! Why won't tit pass the variables. I need help!Poised
Hmm... I replaced the request() with Upload.Form() and now it is uploading the image and creating the record in the database... YAY!!! But, i'm not getting the name of the file for the database. Just this last little problem and it should be good. I have tried "attach1" and/or "fileName" but, I'm not receiving a value for either one. Anyone out there? It's so close!Poised
OMG! I did it! YAY it works!!! Might be a little unorthodox but, muahaha it works!Poised
Guess I spoke to soon again. I'm trying to incorporate the working code into a file that is an include for the main template of the site. However, now I'm getting an error caused by "Option Explicit" that say "Expected Statement". What the what? If I remove it I get a "Syntax Error" for "Class FreeASPUpload". Now what?Poised
It's for real this time. Couldn't include it in the page due to the "Option Explicit" statement. So, instead I put the wooking code on a page that opens inside of an iframe. In Order to pass some of the variables tough I set a cookie to record then the retrieve them during the save() function. Works great know. Thanks for all the help guys! Wait! Nobody helped. How disappointing!Poised

© 2022 - 2024 — McMap. All rights reserved.