• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

asp.net PDF from database???

I have a database that holds a couple pdfs as oledb objects.  When I select everything from the table holding the pdf's how do I then display a pdf on the page or in a new page from the dataset???
0
macros14
Asked:
macros14
  • 4
  • 3
1 Solution
 
davidlars99Commented:
I don't know what your current setup is for the database but this is how you would do it from hard drive


<%@ Import Namespace="System.IO" %>
<%@ Page Language="VB" Debug="true" %>
<HTML>
<HEAD>
</HEAD>
<BODY>
<%
Dim MyFileStream As FileStream
Dim FileSize As Long

MyFileStream = New FileStream(MapPath("mypdfdocument.pdf"), FileMode.Open)
FileSize = MyFileStream.Length

Dim Buffer(CInt(FileSize)) As Byte
MyFileStream.Read(Buffer, 0, CInt(FileSize))
MyFileStream.Close()

Response.ContentType = "application/pdf"
Response.OutputStream.Write(Buffer, 0, FileSize)
Response.Flush()
Response.Close()

%>
</BODY>
</HTML>
0
 
davidlars99Commented:
eventualy you would use "Stream" class instead of "FileStream" and keep in mind that when you'll be creating a datareader you must do this for performance's sake

Dim rdr as SqlDataReader=cmd.ExecuteReader(CommandBehavior.SequentialAccess)

"CommandBehavior.SequentialAccess" is recommended when selecting BLOB data from the database
0
 
dwcorwithCommented:
You will need Adobe Reader on the webserver as well?  I'm not sure of this however worth checking if its not working for you.
regards,
dwcorwith
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
macros14Author Commented:
I need to know how to stream the bytes out of my dataset field rather then from a file path.  Any suggestions or another option??
0
 
davidlars99Commented:
<%@ Import Namespace="System.IO" %>
<%@ Page Language="VB" Debug="true" %>
<HTML>
<HEAD>
</HEAD>
<BODY>
<%
Dim MyFileStream As Stream
Dim FileSize As Long

MyFileStream = New Stream(dataset_field)
FileSize = MyFileStream.Length

Dim Buffer(CInt(FileSize)) As Byte
MyFileStream.Read(Buffer, 0, CInt(FileSize))
MyFileStream.Close()

Response.ContentType = "application/pdf"
Response.OutputStream.Write(Buffer, 0, FileSize)
Response.Flush()
Response.Close()

%>
</BODY>
</HTML>
0
 
macros14Author Commented:
I get this error for instantiating the filestream

c:\inetpub\wwwroot\Church\pdf.aspx.vb(49): 'New' cannot be used on a class that is declared 'MustInherit'.
0
 
davidlars99Commented:
my mistake, instead of "Stream" use "BinaryReader"
0
 
macros14Author Commented:
Also get an error when it is loading saying the pdf is invalid, must have ole object header on it.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now