Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (43)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)

Main Category  Other
  Website Maintenance (3)
Log In
UserName Or Email:

Password:

Auto-Login:

Hosted by Securewebs.com
 
Home : .NET : ADO : Uploading Files into an Access Database using plain ASP
 
Read full length articles at Stardeveloper using Twitter Follow on Twitter Facebook Facebook fan page Email Get Articles via Email RSS Get Articles via RSS Feed
<%
  ' -- Loader.asp --
  ' -- version 1.5.2
  ' -- last updated 12/5/2002
  '
  ' Faisal Khan
  ' faisal@stardeveloper.com
  ' www.stardeveloper.com
  ' Class for handling binary uploads

  Class Loader
    Private dict
    
    Private Sub Class_Initialize
      Set dict = Server.CreateObject("Scripting.Dictionary")
    End Sub

    Private Sub Class_Terminate
      If IsObject(intDict) Then
        intDict.RemoveAll
        Set intDict = Nothing
      End If
      If IsObject(dict) Then
        dict.RemoveAll
        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")
        Else
        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
      Else
        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)))
            Next
            file.Close
          saveToFile = True
      Else
          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)
      Else
        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)
      Else
        getFilePath = ""
      End If
    End Function

    Public Function getFilePathComplete(name)
      If dict.Exists(name) Then
        getFilePathComplete = dict(name).Item("FileName")
      Else
        getFilePathComplete = ""
      End If
    End Function

    Public Function getFileSize(name)
      If dict.Exists(name) Then
        getFileSize = LenB(dict(name).Item("Value"))
      Else
        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"  
          Else
            temp = FormatNumber((temp / 1024), 2)
            getFileSizeTranslated = temp & " kilobytes"
          End If
      Else
        getFileSizeTranslated = ""
      End If
    End Function

    Public Function getContentType(name)
      If dict.Exists(name) Then
        getContentType = dict(name).Item("ContentType")
      Else
        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))
          Else
            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
    Wend
  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))
     Next
  End Function

  Private Function byteToString(toConv)
    For i = 1 to LenB(toConv)
      byteToString = byteToString & Chr(AscB(MidB(toConv,i,1))) 
    Next
  End Function
%>

Explanation
All the ASP code that we will use to insert binary data ( images ) into database is present in this class file ( 'Loader.asp' ). We will be using it's methods to insert and retrieve binary data later.

I haven't explained the code in Loader.asp page, why ? only because it is too lengthy and you'll be better off if you simply use it.

Creating HTML Form
Now open your note pad ( or any other HTML editor you use ) and create a new file. Save it as 'insert.htm'. Now copy the text listed below and paste it in to the newly created 'insert.htm' page and hit the save button :


Previous ( 1 Gone )( 2 Remaining ) Next

Comments/Questions ( Threads: 105, Comments: 213 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Operation Not Allowed Error ( 1 Reply )
  2. Unknown Internal server error 80004005
  3. Uploading Files into an Access Database using plain ASP
  4. Uploaded PDF files keep asking to
  5. Displaying the image
  6. Error in Line 104 in Insert.Asp
  7. Fantastic Script!!!!!
  8. vb.net ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  9. ASP
  10. Cannot update. Database or object is read-only.
  11. Error when passing multiple list values
  12. Increasing Max File Size
  13. HTTPS
  14. Retrieve binary data an saving into a folder from DB using asp
  15. Deleting Files
  16. insert exist image on server to database
  17. Solution for error: ADODB.Recordset error '800a0cc1'
  18. Huge File Upload?? up to 2GB?? ( 1 Reply )
  19. Nice.. but.. I found out that some JPEG files are incomplete. ( 1 Reply )
  20. INSERT.asp ERROR (Image DB upload) ( 1 Reply )
  21. Making file upload unrequired ( 1 Reply )
  22. about calendar in asp.net
  23. Setting file names on saving.
  24. Size of the image uploaded to the SQL database ( 2 Replies )
  25. Downloading data from a database
  26. Large images are not displayed ( 1 Reply )
  27. JPG Files won't upload ( 1 Reply )
  28. How to load many files at a time ???
  29. Upload without using the FileSystemObject ( 1 Reply )
  30. Posting other Binary data problems ( 4 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  31. tried the insert image but... ( 3 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  32. Error Using
  33. Excellent info. Thanks...
  34. Help using this with MySQL
  35. Please Help!!!
  36. How could I upload files from a certain folder (not manual selection)?
  37. Using Oracle ( 1 Reply )
  38. using sql stored procedure to upload file ( 2 Replies )
  39. How can I download the files as the same file name as they were uploaded. ( 1 Reply )
  40. !! UPDATING BINARY DATA !! ..HELP
  41. My images get uploaded but cannot be displayed...help ( 1 Reply )
  42. file names ( 1 Reply )
  43. duplicate file names
  44. Multiple Image Files (very important)
  45. insert images into DB as link? ( 1 Reply )
  46. Defaulting files
  47. The page cannot be displayed on insert.asp ( 3 Replies )
  48. Upload MS-Word documents into an access database
  49. Error please hellllup
  50. form data not coming in
  51. about zipping the size of binary object
  52. Upload text fields without binary data ( 2 Replies )
  53. Server error: Unable to retrieve schema information
  54. SQL syntex for upload imag file to MySQL ( 2 Replies )
  55. txt and asp files
  56. Translated appendchunk method looses odd byte, results in partial data or corruption
  57. Truncated jpeg images with odd byte size
  58. Cannot read array of inputs
  59. Wrong data size in Access db ( 3 Replies )
  60. It was a great help!!!
  61. Inserting Images ( binary data ) into Database ( 2 Replies )
  62. Inserting Images ( binary data ) into Database
  63. No File was Selected
  64. Reading Image from database
  65. Zip Files error ( 5 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  66. Overwriting/updating existing fields ( 2 Replies )
  67. Getting Page Cannot be displayed error message ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  68. Limit image size through maximizing width and Heigth ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  69. Great Code! ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  70. Changing binary pictures in existing database ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  71. Images do not upload!!!
  72. Having troubles with inserting images ( 3 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  73. First Class Tutorial ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  74. Upload files to server hard disk
  75. Can i Upload 2 Images at one time?
  76. Invalid procedure call or argument: 'MidB' ( 3 Replies )
  77. 'ASP 0177 : 800a01c9'
  78. How to Update Images ( binary data ) into Database ( 3 Replies )
  79. Upload to database Where ID = ( 2 Replies )
  80. gives me error when i try to open the binary file from access ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  81. resizing images ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  82. uploading multiple files
  83. Categorizing Files ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  84. My images get uploaded but cannot be displayed...help ( 4 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  85. how to see the uploaded file (pls help) ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  86. how about using oracle database when storing binary data ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  87. Can ASP & ORACLE WORK TOGETHER ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  88. How to delete? ( 3 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  89. error when there is no file being uploaded ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  90. Limiting File size and file type ( 3 Replies )
  91. inserting and displaying images in ASP.NET
  92. inserting image in database
  93. Displaying the Image!!!!! ( 4 Replies )
  94. Using MySQL ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  95. need some clarification ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  96. Loader.asp ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  97. Tutorial !!!!!! ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  98. Error: Cannot update. ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  99. Passing Parameters to the upload page. ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  100. SQL Dbase ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  101. Database ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  102. Uploading status bar ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  103. Height & Width ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  104. 0x80004005 ( 3 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  105. Excellent Tutorial ( 7 Replies ) This thread contains 3 replies by the Author of this Article. This thread contains 3 replies by Faisal Khan.
  106. securing access database ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.