ASP- Image upload to access database

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: ASP- Image upload to access database

  1. #1
    Join Date
    Nov 2006
    India, Kerala

    ASP- Image upload to access database

    i am new to asp and no idea about image upload ,so i followed one tutorial from which is written by faisel khan. That code is working fine,. but now my html form have one listbox control where the user can select multiple values,In this case when the user select more than one value in the list item my code is not working. and i have no idea about what modification have to be done . any suggestions.
    or is there any other way for doing the same. any other tutorials?.
    i am posting my code here.
    This is my html form

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns="">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script language="JavaScript" type="text/javascript">
    function checkform ( form )
      if (form.txtName.value == "") {
        alert( "Please enter your name " );
        return false ;
       if (form.txtEmail.value == "") {
        alert( "Please Enter your Email Address " );
        return false ;
      if (form.lstDescription.value == "") {
        alert( "Please Select Project Description " );
        return false ;
      return true ;
    <form method="POST" enctype="multipart/form-data" action="insert_request_quote.asp" onsubmit="return checkform(this);">
    <table width="472" border="1">
      <tr valign="top">
        <td width="279" class="style2">Your name <font color="red">*</font><br /></td>
           <td width="254"><input class="inputbox" maxlength="50" size="35" 
                      name="txtName" /></td></tr>
      <tr valign="top">
                  <td class="style2">Your email address <font color="red">*</font><br /></td>
                  <td><input class="inputbox" maxlength="50" size="35" 
                    name="txtEmail" /></td>
      <tr valign="top">
        <td><span class="style2">Please select to best describe this project <font 
                      color="red">*</font><br />
        </span><span class="style13"><small>Use the CTRL key to select all that apply</small></span></td>
                  <td><select multiple="multiple" size="6" 
                    <option value="">-------- Select all that apply ---------</option>
                        value="Develop a new site">Develop a new site</option>
                        value="Redesign an existing site">Redesign an existing site</option>
                        value="Subcontract web design or programming">Subcontract design or programming</option>
                        value="Make existing site accessible (ADA)">Make existing site accessible (ADA)</option>
                        value="Make existing site multilingual">Make existing site multilingual</option>
                    <option value="other (specify below)">Other (specify below)</option>
      <td class="style2">You can upload a spec document, file or image from your computer</td>
                  <td><input class="inputbox" type="file" size="30" 
    name="uploads" /></td>
                  <td height="50" align="middle">&nbsp;</td>
                  <td height="50" align="middle">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <div align="left">
                          <input class="command_button" type="submit" value="Submit" name="submitbt" />
                      height="8" alt="" 
                      src="Custom quote - the best prices on web site design from India_files/blank.gif" 
                      width="20" border="0" />
                          <input name="reset" type="reset" class="command_button" value="Reset" />
    THis is my asp code

    <% ' insert_request_quote.asp %>
    <!--#include file="Loader.asp"-->
      Response.Buffer = True
      ' load object
      Dim load
        Set load = new Loader
        ' calling initialize method
      ' File binary data
      Dim fileData
        fileData = load.getFileData("uploads")
      ' File name
      Dim fileName
        fileName = LCase(load.getFileName("uploads"))
      ' File path
      Dim filePath
        filePath = load.getFilePath("uploads")
      ' File path complete
      Dim filePathComplete
        filePathComplete = load.getFilePathComplete("uploads")
      ' File size
      Dim fileSize
        fileSize = load.getFileSize("uploads")
      ' File size translated
      Dim fileSizeTranslated
        fileSizeTranslated = load.getFileSizeTranslated("uploads")
      ' Content Type
      Dim contentType
        contentType = load.getContentType("uploads")
      ' No. of Form elements
      Dim countElements
        countElements = load.Count
      ' Value of text input field "name"
      Dim nameInput
        nameInput = load.getValue("txtName")
      ' Value of text input field "email"
      Dim EmailAddress
        EmailAddress = load.getValue("txtEmail")
    	Dim projectDescription
        projectDescription = load.getValue("lstDescription") 
      ' destroying load object
      Set load = Nothing
      ' Connection string
          Dim connStr
            connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
            connStr = connStr & Server.MapPath("db/requestAQuote.mdb")
          ' Recordset object
          Dim rs
            Set rs = Server.CreateObject("ADODB.Recordset")
            rs.Open "requestQuote", connStr, 2, 2
        ' Checking to make sure if file was uploaded
        If fileSize > 0 Then
            ' Adding data
              rs("Name") = nameInput
              rs("EmailAddress") = EmailAddress
    		  rs("DescriptionOfProject") = projectDescription
    		   rs("Upload").AppendChunk fileData
    		  rs("File Name") = fileName
              rs("File Size") = fileSize
    		  rs("Content Type") = contentType		  
            Set rs = Nothing     
          Response.Write "<font color=""green"">File was successfully uploaded..."
          Response.Write "</font>"
          Response.Write "<font color=""brown"">No File Selected For Uploading"
          Response.Write "...</font>"
        End If           
        If Err.number <> 0 Then
          Response.Write "<br><font color=""red"">Something went wrong..."
          Response.Write "</font>"
        End If

  2. #2
    Join Date
    Nov 2006
    India, Kerala
    And code for Loader.asp is

      Class Loader
        Private dict
        Private Sub Class_Initialize
          Set dict = Server.CreateObject("Scripting.Dictionary")
        End Sub
        Private Sub Class_Terminate
          If IsObject(intDict) Then
            Set intDict = Nothing
          End If
          If IsObject(dict) Then
            Set dict = Nothing
          End If
        End Sub
        Public Property Get Count
          Count = dict.Count
        End Property
        Public Sub Initialize
          If Request.TotalBytes > 0 Then
            Dim binData
              binData = Request.BinaryRead(Request.TotalBytes)
              getData binData
          End If
        End Sub
        Public Function getFileData(name)
          If dict.Exists(name) Then
            getFileData = dict(name).Item("Value")
            getFileData = ""
          End If
        End Function
        Public Function getValue(name)
          Dim gv
          If dict.Exists(name) Then
            gv = CStr(dict(name).Item("Value"))
            gv = Left(gv,Len(gv)-2)
            getValue = gv
            getValue = ""
          End If
        End Function
        Public Function saveToFile(name, path)
          If dict.Exists(name) Then
            Dim temp
              temp = dict(name).Item("Value")
            Dim fso
              Set fso = Server.CreateObject("Scripting.FileSystemObject")
            Dim file
              Set file = fso.CreateTextFile(path)
                For tPoint = 1 to LenB(temp)
                    file.Write Chr(AscB(MidB(temp,tPoint,1)))
              saveToFile = True
              saveToFile = False
          End If
        End Function
        Public Function getFileName(name)
          If dict.Exists(name) Then
            Dim temp, tempPos
              temp = dict(name).Item("FileName")
              tempPos = 1 + InStrRev(temp, "\")
              getFileName = Mid(temp, tempPos)
            getFileName = ""
          End If
        End Function
        Public Function getFilePath(name)
          If dict.Exists(name) Then
            Dim temp, tempPos
              temp = dict(name).Item("FileName")
              tempPos = InStrRev(temp, "\")
              getFilePath = Mid(temp, 1, tempPos)
            getFilePath = ""
          End If
        End Function
        Public Function getFilePathComplete(name)
          If dict.Exists(name) Then
            getFilePathComplete = dict(name).Item("FileName")
            getFilePathComplete = ""
          End If
        End Function
        Public Function getFileSize(name)
          If dict.Exists(name) Then
            getFileSize = LenB(dict(name).Item("Value"))
            getFileSize = 0
          End If
        End Function
        Public Function getFileSizeTranslated(name)
          If dict.Exists(name) Then
            temp = LenB(dict(name).Item("Value"))
              If temp <= 1024 Then
                getFileSizeTranslated = temp & " bytes"  
                temp = FormatNumber((temp / 1024), 2)
                getFileSizeTranslated = temp & " kilobytes"
              End If
            getFileSizeTranslated = ""
          End If
        End Function
        Public Function getContentType(name)
          If dict.Exists(name) Then
            getContentType = dict(name).Item("ContentType")
            getContentType = ""
          End If
        End Function
      Private Sub getData(rawData)
        Dim separator 
          separator = MidB(rawData, 1, InstrB(1, rawData, ChrB(13)) - 1)
        Dim lenSeparator
          lenSeparator = LenB(separator)
        Dim currentPos
          currentPos = 1
        Dim inStrByte
          inStrByte = 1
        Dim value, mValue
        Dim tempValue
          tempValue = ""
        While inStrByte > 0
          inStrByte = InStrB(currentPos, rawData, separator)
          mValue = inStrByte - currentPos
          If mValue > 1 Then
            value = MidB(rawData, currentPos, mValue)
            Dim begPos, endPos, midValue, nValue
            Dim intDict
              Set intDict = Server.CreateObject("Scripting.Dictionary")
              begPos = 1 + InStrB(1, value, ChrB(34))
              endPos = InStrB(begPos + 1, value, ChrB(34))
              nValue = endPos
            Dim nameN
              nameN = MidB(value, begPos, endPos - begPos)
            Dim nameValue, isValid
              isValid = True
              If InStrB(1, value, stringToByte("Content-Type")) > 1 Then
                begPos = 1 + InStrB(endPos + 1, value, ChrB(34))
                endPos = InStrB(begPos + 1, value, ChrB(34))
                If endPos = 0 Then
                  endPos = begPos + 1
                  isValid = False
                End If
                midValue = MidB(value, begPos, endPos - begPos)
                  intDict.Add "FileName", trim(byteToString(midValue))
              begPos = 14 + InStrB(endPos + 1, value, stringToByte("Content-Type:"))
              endPos = InStrB(begPos, value, ChrB(13))
                midValue = MidB(value, begPos, endPos - begPos)
                  intDict.Add "ContentType", trim(byteToString(midValue))
                begPos = endPos + 4
                endPos = LenB(value)
                nameValue = MidB(value, begPos, ((endPos - begPos) - 1))
                nameValue = trim(byteToString(MidB(value, nValue + 5)))
              End If
              If isValid = True Then
                intDict.Add "Value", nameValue
                intDict.Add "Name", nameN
                dict.Add byteToString(nameN), intDict
              End If
          End If
          currentPos = lenSeparator + inStrByte
      End Sub
      End Class
      Private Function stringToByte(toConv)
        Dim tempChar
         For i = 1 to Len(toConv)
           tempChar = Mid(toConv, i, 1)
          stringToByte = stringToByte & chrB(AscB(tempChar))
      End Function
      Private Function byteToString(toConv)
        For i = 1 to LenB(toConv)
          byteToString = byteToString & Chr(AscB(MidB(toConv,i,1))) 
      End Function

Similar Threads

  1. Save Image to Field
    By Chicho in forum VB Classic
    Replies: 11
    Last Post: 10-01-2003, 01:00 AM
  2. Replies: 0
    Last Post: 01-16-2002, 08:03 PM
    By greg greg32HOME.COM in forum ASP.NET
    Replies: 1
    Last Post: 01-04-2002, 09:51 PM
  4. ASP and Access Database on CD
    By Mike H in forum VB Classic
    Replies: 4
    Last Post: 03-20-2001, 10:50 AM
  5. converse asp code for access database from sql
    By deane in forum authorevents.mitchell
    Replies: 0
    Last Post: 10-16-2000, 11:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center