473,385 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

.NET 2.0 Web App - streaming PDF

DB
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

Mar 16 '06 #1
1 5376
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" <DB@discussions.microsoft.com> wrote in message
news:43**********************************@microsof t.com...
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

Mar 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: googlegroups | last post by:
I am playing with the XMLHTTPRequest method to perform client/server transactions. I have it set up right now so that when readyState is 4, it takes the XML and processes it. This works great until...
5
by: John | last post by:
Hi all, I have an (well, what I think to be, at least) interesting question: Is it possible to stream data down to the client and, after a certain amount of data has been streamed, allow the...
3
by: A.M-SG | last post by:
Hi, I have a ASP.NET aspx file that needs to pass large images from a network storage to client browser. The requirement is that users cannot have access to the network share. The aspx file...
2
by: mpaliath | last post by:
Hi guys I am currently involved in a project which requires me to recieve and play streaming video as well as send it. In Visual C++ is there any free library which helps me do this as...
8
by: Amjad | last post by:
Hi i am writing a application where i want to browse video file and copy data into stream and send that stream over network...I have develop P2P windows application where i successfully transfer...
2
by: SPG | last post by:
Hi, Two questions for you all.. Firstly, is there a way of streaming video using PHP? At the moment I just have a link to a video file and the whole thing downloads before playing which is a...
8
by: Tony K | last post by:
Is streaming audio capable in asp.net 2.0 / AJAX? What I mean by streaming audio is: ability to play one or more songs back to back...or maybe even let the user select several songs to play and it...
5
by: pmakoi | last post by:
dear all this might be a piece of cake for some of you out there but it is causing me a lot of stress given the fact that there is not enogh documentation out there regarding this topic I am...
3
by: Brad | last post by:
I have an aspx page that is sending pdf files to client browsers: it uses a filestream to read the pdf file and response.binarywrite to send content to the browser. This has worked great for years...
1
by: Faisal Shafiq | last post by:
I want to upload a file direct to the Silverlight Streaming Service from a Web Client such as silverlight application. As per our product requirement we want to upload a .WMV file directly from...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.