"stream" a PDF?

  • Thread starter Thread starter Tom Kaminski [MVP]
  • Start date Start date
I'm not sure why you would need or want to do this. A browser downloads
files. A PDF document is a file. A browser can view a PDF document (if the
user is wanting a PDF document, I would have to assume that they have
Acrobat installed on their machine). So, you would load a PDF document into
a browser in the same way you would load an HTML or Text document, or any
other format that the browser recognizes: link to it, or use JavaScript to
set the location attribute of the currently loaded document to the location
of the PDF file. Or use link or JavaScript to load the PDF file into a new
browser instance. No streaming or sophisticated code needed.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
Kevin said:
I'm not sure why you would need or want to do this. A browser
downloads files. A PDF document is a file. A browser can view a PDF
document (if the user is wanting a PDF document, I would have to
assume that they have Acrobat installed on their machine). So, you
would load a PDF document into a browser in the same way you would
load an HTML or Text document, or any other format that the browser
recognizes: link to it, or use JavaScript to set the location
attribute of the currently loaded document to the location of the PDF
file. Or use link or JavaScript to load the PDF file into a new
browser instance. No streaming or sophisticated code needed.

Linking works only if the requested file is accessible within a virtual
directory. If the PDF comes from a database, you need streaming (or you have
to store the file temporarily in a virtual directory... messy).

Anyway, streaming a PDF isn't really different from streaming any other
media type back to a web client:

1. Set the Content-Type header.
2. Set the Content-Length header, if available (*very* important for older
Acrobat plug-ins)
3. Set the Content-Disposition header for a proper client-side "Save As"
prompt.
4. Write the data back to the response stream.

Cheers,
 
Kevin Spencer said:
I'm not sure why you would need or want to do this.

Because I want to protect the PDFs with a custom authentication scheme and
do not want to provide a direct URL for the document. The authenticaiton is
handled within the code and thus cannot protect static files if they are
called directly.

--
Tom Kaminski IIS MVP
http://www.microsoft.com/windowsserver2003/community/centers/iis/

http://www.iisfaq.com/
http://www.iistoolshed.com/ - tools, scripts, and utilities for running IIS
http://www.tryiis.com
 
Understood.

Joerg gave you the details. If you have any further trouble, come on back.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
Hi Tom,

If you can get the data into a Byte array, you can shoot it out with
BinaryWrite.

HOW TO: Read and Write a File to and from a BLOB Column by Using ADO.NET and
Visual Basic .NET

http://support.microsoft.com/default.aspx?scid=kb;en-us;316887

Here's some code I have kicking around:

Dim strFileNameToDisplay As String
Dim byteTemp As Byte()
strFileNameToDisplay = Path.GetFileName(strFileName)
Try
' Put the document bytes into the byte array
byteTemp = ' Get the PDF file here see
http://support.microsoft.com/default.aspx?scid=kb;en-us;316887
' If all went well, write the content out as a file
Response.Buffer = True
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", _
"attachment;filename=" & strFileNameToDisplay)
Response.BinaryWrite(byteTemp)
Response.End()
strFileName = ""
Catch exc As Exception
Response.Write("Sorry, unable to retrieve the file at this
time.")
End Try
 
Ken Cox said:
Hi Tom,

If you can get the data into a Byte array, you can shoot it out with
BinaryWrite.

Thanks Ken! BinaryWrite was the part I was missing (duh)!
Here's the code that works:

If File.Exists(sFile) Then

Response.Buffer = True

Response.Clear()

Response.ClearContent()

Response.ClearHeaders()

Response.ContentType = "application/pdf"

Response.AddHeader("Content-Disposition", "attachment; filename=" &
Request("tn") & ".pdf")

Dim fs As FileStream

Dim br As BinaryReader

fs = New FileStream(sFile, FileMode.Open)

br = New BinaryReader(fs)

Dim dataBytes As Byte() = br.ReadBytes(fs.Length - 1)



Response.BinaryWrite(dataBytes)

br.Close()

fs.Close()

End If

--
Tom Kaminski IIS MVP
http://www.microsoft.com/windowsserver2003/community/centers/iis/

http://www.iisfaq.com/
http://www.iistoolshed.com/ - tools, scripts, and utilities for running IIS
http://www.tryiis.com
 
Back
Top