ASP Upload File and Add database fields

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saldandm
    New Member
    • Nov 2007
    • 4

    ASP Upload File and Add database fields

    I think this is probably some minor oversight on my end but I'm just missing it.

    I have a multipart/form-data form in a ASP page. Inside the form I have traditional text fields and a upload field for uploading images to a server.

    The intent is to store the image name (not the image itself) along with corresponding data in a SQL Server database.

    The problem I'm currently having is that the upload is working correctly but it is not writing any data into the database. The page does not report an error it just simply does not write. It's like the page the form submits to isn't getting the data from the form because nothing is in the fields when I do a
    Code:
     response.write
    .

    Here's the code. Any help is greatly appreciated.

    Code:
          Title = Request("Title")
          ShortTitle = Request("ShortTitle")
          SubmittedBy = Request("SubmittedBy")
          StoryDate = Request("StoryDate")
          StoryEndDate = Request("StoryEndDate")
          Story = Request("Story")
    	  
    	  'Response.Write story
    	  if instr(1,Story,"<P>") = 1 then
    	    	Story = mid(Story,4,len(Story))
    	  end if
        
    	  Set StrConnection = Server.CreateObject("ADODB.Connection")
    	  Set StrRecordSet = Server.CreateObject("ADODB.Recordset")
    	  StrConnection.Open "Provider=MSDASQL;Driver={SQL Server};SERVER=FF-CORPSQL;DATABASE=testannouncements;uid=intranet;pwd=reltd;"
    	  StrSql = "SELECT * FROM announcements;"
    
          StrRecordSet.Open StrSql, StrConnection,1,2
            if Title <> "" then
    		with StrRecordSet
    	          .AddNew 
    	               Title= Replace(Title, "'", "^")
    	               StrRecordSet("Title")=Title
                          ShortTitle= Replace(ShortTitle, "'", "^")
    	               StrRecordSet("ShortTitle")=ShortTitle
    
    	               SubmittedBy= Replace(SubmittedBy, "'", "^")
                       StrRecordSet("SubmittedBy")=SubmittedBy
    	               StoryDate= Replace(StoryDate, "'", "^")
                       StrRecordSet("StoryDate")=StoryDate
                       'StoryEndDate= Replace(StoryEndDate, "'", "^")
                       if StoryEndDate <> "" then
    						StrRecordSet("StoryEndDate")=StoryEndDate
    				   end if
    	               Story= Replace(Story, "'", "^")
    	               StrRecordSet("Department") = Request("Department")
    	               StrRecordSet("Story")=Story
    	               'if filename = "" then
    					'	If Request("StoryPic") <> "None" then
    					'		StrRecordSet("picture")=Request("StoryPic")
    					'	end if
    				   'else
    					'	StrRecordSet("picture")=filename
    				   'end if	
                     .Update 
    		end with
    	end if
        set strrecordset = nothing
        StrConnection.Close 
        set StrConnection = nothing
        %>
        <!--#include file="upload.asp" -->
        <%
        Dim Uploader, File, ErrMsg
    	Set Uploader = New FileUploader
    	' This starts the upload process
    	Uploader.Upload()
    	
    	'******************************************
    	' Use [FileUploader object].Form to access 
    	' additional form variables submitted with
    	' the file upload(s). (used below)
    	'******************************************
    
    	' Check if any files were uploaded
    	If Uploader.Files.Count = 0 Then
    	'	Response.Redirect("add_news.asp")
    	Else
    		' Loop through the uploaded files
    		For Each File In Uploader.Files.Items
    		
    			' Check where the user wants to save the file
    			If Uploader.Form("saveto") = "disk" Then
    				' Save the file
    				File.SaveToDisk strDISKLOCATION 
    			End If
    			Dim filename
    			filename = File.FileName '& "<br>"
    		Next
    	End If
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    First do a basic troubleshoot to see if the form data made it:
    [code=asp]for each x in request.form
    response.write x & ": " & request.form(x) & "<br>" & vbNewline
    next[/code]Try it and tell me what you get.

    Jared

    Comment

    • saldandm
      New Member
      • Nov 2007
      • 4

      #3
      Sorry for the delay in my response.

      I added the code and nothing is written

      Comment

      • jhardman
        Recognized Expert Specialist
        • Jan 2007
        • 3405

        #4
        Originally posted by saldandm
        Sorry for the delay in my response.

        I added the code and nothing is written
        Then the form data is not being sent. post the code for your form.

        Jared

        Comment

        • saldandm
          New Member
          • Nov 2007
          • 4

          #5
          Code:
                 <form name="LayoutRegion1FORM" enctype="multipart/form-data" action="/admin/test/html/announcements/addnewsresult.asp" method="POST">
                  <table border="0" cellspacing="0" cellpadding="0">
                   <tr valign="top" align="left">
                    <td height="24"></td>
                   </tr>
                   <tr valign="top" align="left">
                    <td width="557">
                     <table id="Table8" border="0" cellspacing="2" cellpadding="2" width="100%" style="height: 276;">
                      <tr style="height: 26px;">
                       <td width="126">
                        <p style="text-align: right;"><b><span style="font-size: x-small;">Title:</span></b></p>
                       </td>
                       <td width="417">
                        <p><input id="FormsEditField6" type="text" name="Title" style="white-space:pre" value="" size="42" maxlength="500"></p>
                       </td>
                      </tr>
                      <tr style="height: 26px;">
                       <td>
                        <p style="text-align: right;"><b><span style="font-size: x-small;">Short Title:</span></b></p>
                       </td>
                       <td>
                        <p><input id="FormsEditField9" type="text" name="ShortTitle" style="white-space:pre" value="" size="20" maxlength="20"></p>
                       </td>
                      </tr>
                      <tr style="height: 26px;">
                       <td>
                        <p style="text-align: right;"><b><span style="font-size: x-small;">Date:</span></b></p>
                       </td>
                       <td>
                        <p><input id="FormsEditField7" type="text" name="StoryDate" style="white-space:pre" value="" size="23" maxlength="50"></p>
                       </td>
                      </tr>
                      <tr style="height: 26px;">
                       <td>
                        <p style="text-align: right;"><b><span style="font-size: x-small;">Submitted By:</span></b></p>
                       </td>
                       <td>
                        <p><input id="txtID" type="text" name="submittedby" style="white-space: pre;" value="<%= strhold %>" size="23" maxlength="23"></p>
                       </td>
                      </tr>
                      <tr style="height: 26px;">
                       <td>
                        <p style="text-align: right;"><b><span style="font-size: x-small;">Department:</span></b></p>
                       </td>
                       <td>
                        <p><input id="Department" type="text" name="Department" style="white-space:pre" value="" size="23" maxlength="50"></p>
                       </td>
                      </tr>
                      <tr style="height: 146px;">
                       <td>
                        <p>&nbsp;</p>
                       </td>
                       <td>
                        <p>&nbsp;</p>
                        <p><input type="button" name="button1" value="Bold" id="button1" ONCLICK="insertString('<B>')">&nbsp;<input type="button" name="button2" value="Stop Bold" id="button2" ONCLICK="insertString('</B>')"><br><input type="button" name="button1" value="Italics" id="FormsButton5" ONCLICK="insertString('<I>')">&nbsp;<input type="button" name="button2" value="Stop Italics" id="FormsButton6" ONCLICK="insertString('</I>')"><br><input type="button" name="button1" value="Underline" id="FormsButton7" ONCLICK="insertString('<U>')">&nbsp;<input type="button" name="button2" value="Stop Underline" id="FormsButton8" ONCLICK="insertString('</U>')"><br><input type="button" name="button4" value="New Paragraph" id="FormsButton9" ONCLICK="insertString('<P>')"></p>
                       </td>
                      </tr>
                      <tr style="height: 246px;">
                       <td>
                        <p style="text-align: right;"><b><span style="font-size: x-small;">Story:</span></b></p>
                       </td>
                       <td>
                        <p><textarea id="Story" name="Story" style="white-space: pre;" rows="15" cols="50" onclick=setCursorPos() onchange=setCursorPos()></textarea></p>
                       </td>
                      </tr>
                     </table>
                    </td>
                   </tr>
                  </table>
                  <table border="0" cellspacing="0" cellpadding="0" width="464">
                   <tr valign="top" align="left">
                    <td width="134" height="25"><img src="../../assets/images/autogen/clearpixel.gif" width="134" height="1" border="0" alt=""></td>
                    <td></td>
                   </tr>
                   <tr valign="top" align="left">
                    <td height="22"></td>
                    <td width="330"><input type="file" name="FILE1" size="50"><input TYPE="radio" NAME="saveto" value="disk" checked></td>
                   </tr>
                  </table>
                  <table border="0" cellspacing="0" cellpadding="0">
                   <tr valign="top" align="left">
                    <td width="20" height="10"><img src="../../assets/images/autogen/clearpixel.gif" width="20" height="1" border="0" alt=""></td>
                    <td></td>
                   </tr>
                   <tr valign="top" align="left">
                    <td></td>
                    <td width="553">
                     <table id="Table9" border="1" cellspacing="1" cellpadding="1" width="100%" style="height: 86;">
                      <tr align="center" style="height: 100px;">
                       <td width="105">
                        <p style="text-align: center;">-None-</p>
                       </td>
                       <td width="105">
                        <p style="text-align: center;"><img id="Picture4" height="100" width="81" src="../../assets/images/1.gif" vspace="0" hspace="0" align="TOP" border="0" alt="1" title="1"></p>
                       </td>
                       <td width="105">
                        <p style="text-align: center;"><img id="Picture5" height="100" width="81" src="../../assets/images/2.gif" vspace="0" hspace="0" align="TOP" border="0" alt="2" title="2"></p>
                       </td>
                       <td width="105">
                        <p style="text-align: center;"><img id="Picture6" height="100" width="81" src="../../assets/images/3.gif" vspace="0" hspace="0" align="TOP" border="0" alt="3" title="3"></p>
                       </td>
                       <td width="105">
                        <p style="text-align: center;"><img id="Picture7" height="100" width="81" src="../../assets/images/4.gif" vspace="0" hspace="0" align="TOP" border="0" alt="4" title="4"></p>
                       </td>
                      </tr>
                      <tr align="center" style="height: 19px;">
                       <td>
                        <p><input id="FormsRadioButton1" type="radio" CHECKED name="StoryPic" value="None">&nbsp;</p>
                       </td>
                       <td>
                        <p><input id="FormsRadioButton2" type="radio" name="StoryPic" value="image1.gif">&nbsp;</p>
                       </td>
                       <td>
                        <p><input id="FormsRadioButton3" type="radio" name="StoryPic" value="image2.gif">&nbsp;</p>
                       </td>
                       <td>
                        <p><input id="FormsRadioButton4" type="radio" name="StoryPic" value="image3.gif">&nbsp;</p>
                       </td>
                       <td>
                        <p><input id="FormsRadioButton5" type="radio" name="StoryPic" value="image4.gif">&nbsp;</p>
                       </td>
                      </tr>
                     </table>
                    </td>
                   </tr>
                  </table>
                  <table border="0" cellspacing="0" cellpadding="0" width="381">
                   <tr valign="top" align="left">
                    <td width="197" height="19"><img src="../../assets/images/autogen/clearpixel.gif" width="197" height="1" border="0" alt=""></td>
                    <td></td>
                   </tr>
                   <tr valign="top" align="left">
                    <td height="24"></td>
                    <td width="184"><input type="submit" name="Submit" value="Add Announcement" id="FormsButton11"></td>
                   </tr>
                  </table>
                 </form>

          Comment

          • Nicodemas
            Recognized Expert New Member
            • Nov 2007
            • 164

            #6
            I see that you are making requests to the from the Request object. When sending binary data through a form, and making a request to any Request object other than Request.Queryst ring, you cannot access the binary data.

            The Upload API you have provides you with a method to get the binary data. Use that to retrieve all your form values instead of the Request object.

            Instantiate your API first, then do your form value retrievals.

            Comment

            • jhardman
              Recognized Expert Specialist
              • Jan 2007
              • 3405

              #7
              Originally posted by Nicodemas
              I see that you are making requests to the from the Request object. When sending binary data through a form, and making a request to any Request object other than Request.Queryst ring, you cannot access the binary data.

              The Upload API you have provides you with a method to get the binary data. Use that to retrieve all your form values instead of the Request object.

              Instantiate your API first, then do your form value retrievals.
              I'm not sure that's the problem, he says he is trying to save the file name in the db and that is accessible from the request object...

              Comment

              • jhardman
                Recognized Expert Specialist
                • Jan 2007
                • 3405

                #8
                Originally posted by saldandm
                Sorry for the delay in my response.

                I added the code and nothing is written
                you added my code to addnewsresult.a sp? This is just supposed to write the data to the screen, not to the db...

                Comment

                • saldandm
                  New Member
                  • Nov 2007
                  • 4

                  #9
                  Yes, I added it the result page and actually just removed all the other code to see if it was being passed.

                  Nothing was displayed.

                  I actually customized the code I found at this site. And got it working. http://www.codeproject .com/asp/aspupl.asp

                  I would've rather gotten my own working though.

                  Comment

                  • jhardman
                    Recognized Expert Specialist
                    • Jan 2007
                    • 3405

                    #10
                    Originally posted by saldandm
                    Yes, I added it the result page and actually just removed all the other code to see if it was being passed.

                    Nothing was displayed.

                    I actually customized the code I found at this site. And got it working. http://www.codeproject .com/asp/aspupl.asp

                    I would've rather gotten my own working though.
                    Glad you got it working regardless, That is what those sites are for, after all.

                    Jared

                    Comment

                    Working...