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 (41)
  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 : Displaying Images from 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

file.asp
Open notepad and create a new file. Name it as file.asp. Copy the following code and paste it into the newly created file.asp file and hit the save button :

<%
   ' -- file.asp --
   ' Retrieves binary files from the database
   
   Response.Buffer = True
   
   ' ID of the file to retrieve
   Dim ID
      ID = Request("ID")
      
   If Len(ID) < 1 Then
      ID = 7
   End If
   
   ' Connection String
   Dim connStr
      connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
         Server.MapPath("FileDB.mdb")
   
   ' Recordset Object
   Dim rs
      Set rs = Server.CreateObject("ADODB.Recordset")
      
      ' opening connection
      rs.Open "SELECT [File Name], [File Data], [Content Type] FROM Files " & _
            " WHERE ID = " & ID, connStr, 2, 4

      If Not rs.EOF Then
            Response.AddHeader "Content-Disposition", "filename=" & _
                  rs("File Name)
            Response.ContentType = rs("Content Type")
            Response.BinaryWrite rs("File Data")
      End If
      
      
      rs.Close
      Set rs = Nothing
%>

Explanation

<%
	' -- file.asp --
	' Retrieves binary files from the database

	Response.Buffer = True

	' ID of the file to retrieve
	Dim ID
		ID = Request.QueryString("ID")

Sets the buffering to True. Next we create a variable named ID and set it's value to the Request.QueryString("ID").

' Connection String
Dim connStr
	connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
	Server.MapPath("FileDB.mdb")

Next we create a connStr variable as the connection string to the database and set it's path to FileDB.mdb database.

' Recordset Object
Dim rs
	Set rs = Server.CreateObject("ADODB.Recordset")

	' SQL SELECT Statement
	Dim sql_select
	sql_select = "SELECT [File Name], [File Data], [Content Type] "
	sql_select = sql_select & "FROM Files WHERE ID = " & ID

	' opening connection
	rs.Open sql_select, connStr, 2, 4

Create a Recordset object and run a SELECT statement to retrieve the File Name, File Data and Content Type of the given file.

Note that File Data field contains the saved binary data for the file, while Content Type field contains the content type for the file. We need both of them to display the file. The "Content-Disposition" header is necessary to send the file with the correct name back to the browser.

If Not rs.EOF Then
            Response.AddHeader "Content-Disposition", "filename=" & _
                  rs("File Name)
            Response.ContentType = rs("Content Type")
            Response.BinaryWrite rs("File Data")
End If

If Recordset is not empty i.e, there is a file with given ID, then we set the Content Type of given ASP page to file's Content Type. Next we use Response.BinaryWrite method to write the binary data to the client browser. The file will thus be shown to the client.

		rs.Close
		Set rs = Nothing
%>

We close the connection to the database.

On the next page I summarize the steps involved in displaying binary data from the database.

Summary
This article was second in the series of articles about manipulating binary data via ASP and storing it in the database. In this article we built files which were missing in the ASP-Database file uploader application in the first article. show.asp simply displayed a list of all the files in the database. file.asp was the actual file which will send the binary data back to the client.

You must have noticed that how easy it is to write binary data from the database via ASP to the client. There are two things to keep in mind while doing that. First, always properly set the Content Type of the page using Response.ContentType property. Secondly, use one ASP page to display only ONE file like we did with file.asp. And that page should not write anything else to the browser i.e, don't try to write text in an ASP page as we do in other ASP pages when you've already used the Response.BinaryWrite method.

Well that's it for this article. You should continue your reading and read the other two articles of this series; "inserting binary data to the database" and "uploading binary data ( files ) to the server hard disk". That'll be enough.


Previous ( 1 Gone )( No Further Pages )

Download Associated Files
2001033101.zip

Related Articles
  1. How to display records from top 4 database systems using plain ASP?
  2. Uploading Files into an Access Database using plain ASP
  3. Fastest way of Database Access : Caching Records in Memory
  4. Adding records to the database with ASP
  5. DSN vs DSN less Database Connections
  6. Searching and sorting records in a recordset from the database
  7. Speedup Database Access using GetRows
  8. Inserting Form content into Database with ASP
  9. Using ASP pages to page through Recordsets
  10. Generating Random Records from the Database

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

  1. How to open the audio file
  2. Please help
  3. How can i delete the images
  4. having trouble inserting sound files into database ( 3 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  5. Can't view the uploaded files on show.asp
  6. show.asp want to display image rather than hyperlink is it possible?
  7. Cannot upload or display more than 4 megs
  8. multiple database path
  9. help on figuring sql command
  10. Displaying images with file.asp, keep getting a red cross instead of image.
  11. image display problem
  12. Images loose a byte in upload
  13. Response.BinaryWrite PROBLEM !!?? ( 1 Reply )
  14. Updating of Binary Data into the Database
  15. Problem in displaying large files
  16. Some images dont display
  17. Problem with Display/Downloading from the database
  18. About Showing Binary Image from Database ( 1 Reply )
  19. Upload more than one file at a time
  20. Creating HREF Link for image uploaded into DataBAse ( 2 Replies )
  21. How can I copy binary files from database to the Server hard disk?
  22. Images missing bottom part ( 1 Reply )
  23. Displaying Images Inserted Via Access ( 2 Replies )
  24. con.Execute sql_insert
  25. File Retrieval Problem ( 1 Reply )
  26. File.asp not showing image...
  27. Problem retreiving .exe files ( 1 Reply )
  28. problem editing
  29. retrieving images
  30. the display image code is not working
  31. Printing Image
  32. Resizing the images ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  33. Display images
  34. displaying binary images ( 5 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  35. This may be a stupid question ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  36. Having some problem with the BinaryWrite method!!!!!! ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  37. unable to display different kind of files ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  38. How to put html in file.asp ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  39. Displaying file contents ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  40. viewing a doc file shows gibberish ( 3 Replies ) 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.