首页 > 解决方案 > 将记录添加到数据库并同时上传图像(第二次尝试使用不同的代码)

问题描述

有问题!我一遍又一遍地弄乱了这段代码。这只是我的尝试之一。尝试将记录添加到数据库并同时上传图像。我发送的第一个会添加记录,但不会上传图像。这个将上传图像,但不会添加记录。我的耐心快用完了。在这个我收到一个错误,说我不能使用通用请求。

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

标签: ms-accessfile-uploadasp-classic

解决方案


推荐阅读