.NET 2.0 Web App - streaming PDF

G

Guest

Hi All,

I’m trying to stream a PDF with .NET 2.0 in a c# web app. However, it does
not actually show the PDF (using adobe acrobat).

The Situation:

Click on a Button, Opens a new Window, which generates a PDF, then streams
it out to the client. (So the can get the window of acrobate, isnide of IE).

However.. After 5 hours of stuffing around, I am at a loss.

Normally, I would do:

byte[] PDFByteArray = new byte[0];

PDFByteArray = GeneratePDF()

Context.Response.ClearContent();

Context.Response.ClearHeaders();

Context.Response.AddHeader("content-disposition",
"filename=Rename_File.pdf");

Context.Response.ContentType="application/pdf";

Context.Response.AddHeader("content-length",
PDFByteArray.Length.ToString());

Context.Response.BinaryWrite(PDFByteArray);

Context.Response.Flush();

Context.Response.End();

Doing this, does actually send the PDF to IE (using packet sniffers),
however, IE. goes to "about:blank".

However, this is a valid PDF, if i write it to a file, from the sabe byte
array instad of streaming it out, the PDF writes and then opens fine in Adobe.

So I dug a little more and noticed something, which may or may not affect
it. The HTTP headers for the page, in .NET 2.0 are in a different order then
..NET 1.1. Could this be the issue? Everything else is the same, encoding,
content length, etc, except for the .NET framework version. Works in 1.1,
does noting 2.0.

Other paths I have tried.. Creating a HttpWebRequest from the page, and then
streaming that from the other page... same result, PDF is sent to the
browser, but IE, goes to "About:blank" :(

Any ideas, suggestions or comments will be greatly appreciated. At this
stage.. I mean ANYTHING, even random thoughts on the subject as I have
exhausted all avenues I can think of.

Regards,

Daniel Brown
 
K

Ken Cox - Microsoft MVP

Hi Daniel,

I was able to use your code successfully to stream the PDF into the browser
using Visual Web Developer Express. I've included all of the test code,
including a VB class I use to fetch the byte array.

Maybe the bad version is cached somewhere on the client or in your network?
I've had it happen that a bad file sticks in the system and had to rename
pages to get things going again.

Let us know how you make out?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Button1_Click(object sender, EventArgs e)
{
byte[] PDFByteArray = new byte[0];
PDFByteArray = inet.Retrieve("59driveline_task.pdf");
Context.Response.ClearContent();
Context.Response.ClearHeaders();
Context.Response.AddHeader("content-disposition",
"filename=Rename_File.pdf");
Context.Response.ContentType="application/pdf";
Context.Response.AddHeader("content-length",
PDFByteArray.Length.ToString());
Context.Response.BinaryWrite(PDFByteArray);
Context.Response.Flush();
Context.Response.End();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>PDF Array</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:button id="Button1" runat="server" onclick="Button1_Click"
text="Button" /></div>
</form>
</body>
</html>

Imports Microsoft.VisualBasic
Public Class inet
Public Shared Function Retrieve _
(ByVal p_document_name As String) As Byte()
Dim stream1 As IO.FileStream
Dim num2 As Long
Try
stream1 = New IO.FileStream _
(HttpContext.Current.Server.MapPath _
(p_document_name), IO.FileMode.Open)
num2 = stream1.Length
Catch exception2 As Exception
End Try
Dim buffer1 As Byte() = New Byte _
((CType(Conversion.Fix(num2), Integer) + 1) - 1) {}
stream1.Read(buffer1, 0, _
CType(Conversion.Fix(num2), Integer))
stream1.Close()
Return buffer1
End Function
End Class


DB said:
Hi All,

I'm trying to stream a PDF with .NET 2.0 in a c# web app. However, it does
not actually show the PDF (using adobe acrobat).

The Situation:

Click on a Button, Opens a new Window, which generates a PDF, then streams
it out to the client. (So the can get the window of acrobate, isnide of
IE).

However.. After 5 hours of stuffing around, I am at a loss.

Normally, I would do:

byte[] PDFByteArray = new byte[0];

PDFByteArray = GeneratePDF()

Context.Response.ClearContent();

Context.Response.ClearHeaders();

Context.Response.AddHeader("content-disposition",
"filename=Rename_File.pdf");

Context.Response.ContentType="application/pdf";

Context.Response.AddHeader("content-length",
PDFByteArray.Length.ToString());

Context.Response.BinaryWrite(PDFByteArray);

Context.Response.Flush();

Context.Response.End();

Doing this, does actually send the PDF to IE (using packet sniffers),
however, IE. goes to "about:blank".

However, this is a valid PDF, if i write it to a file, from the sabe byte
array instad of streaming it out, the PDF writes and then opens fine in
Adobe.

So I dug a little more and noticed something, which may or may not affect
it. The HTTP headers for the page, in .NET 2.0 are in a different order
then
.NET 1.1. Could this be the issue? Everything else is the same, encoding,
content length, etc, except for the .NET framework version. Works in 1.1,
does noting 2.0.

Other paths I have tried.. Creating a HttpWebRequest from the page, and
then
streaming that from the other page... same result, PDF is sent to the
browser, but IE, goes to "About:blank" :(

Any ideas, suggestions or comments will be greatly appreciated. At this
stage.. I mean ANYTHING, even random thoughts on the subject as I have
exhausted all avenues I can think of.

Regards,

Daniel Brown
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top