Stream pdf to browser

R

Rick

I have a web service that retrieve a pdf file and streams the file into a
buffer passing the buffer on to the calling web page. This works perfect if
I use a button to submit a call to the web service and stream the file into
the calling web form, the problem I have is I need to open the streamed data
in a separate browser window keeping the initial calling web form open
..
I tried using Javascript to open an new window and on page load call the web
service, but doing this returns the buffer but does not post it to the
browser.

Any ideas on how to do this?

Thanks in advance,
Rick
 
S

Steven Cheng[MSFT]

Hi Rick,

From your description, you're using webservice to expose some PDF stream
from backend file or database and stream them to web application(let web
application flush the pdf content to client). currently you're wondering
how to make the web application display the pdf stream download in a
separate browser window from the main window(which trigger the postback)
,correct?

Based on my experience, a typical approach to do this is a below:

1. You should create a separate page which dedicate to retrieving binary
content from the webservice(that return pdf buffer) and stream it out to
web page response). This page will use some url querystring as parameter to
identify which pdf file to retrieve from webservice.

2. In your main web page, when you click a button and postback to retrieve
pdf stream, you just simply register some client-side script that will open
a new browser window, the url is just the dedicated page created in #1 and
append some url querystring (indicate which pdf file to open).

How do you think?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rick

That's what I did the page comes up with no problem the call to the
webservice works, but the file does not show in the browser, if I put a
button on the page that does the same exact call to the web service it works
fine.

Page 1 calls page dedicated to call to web service:
Button2.Attributes.Add("onclick", "window.open('webform2.aspx?f=" &
txtFile.Text & "','mywindow')")

Page2 calls web service and flush to browser:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
getfile()

End If

End Sub

Private Sub getfile()

Dim ffile As FileInfo

Dim ws As webservice

Dim sFile As String = Request.QueryString("f")

Try

ws = New webservice

ws.Credentials = getcredentials

Dim Buffer() As Byte = ws.GetFileBuffer(sFile)

Response.BufferOutput = True

Response.ClearContent()

Response.ClearHeaders()

Response.AddHeader("Content-Length", Buffer.Length.ToString)

Response.AddHeader("content-disposition", "inline;filename=YourReport.pdf")

Response.ContentType = "application/pdf"

Response.BinaryWrite(Buffer)

Response.Flush()

Response.Clear()

Catch ex As Exception

Finally

ffile = Nothing

nref = Nothing

End Try

End Sub
 
S

Steven Cheng[MSFT]

Hi Rick,

Will it work if you directly access that separate aspx page to display the
pdf document?

Also, you can use Response.End instead of Flush to close the response
stream. Here is the test page code I used which work correctly on my side.
You can also have a test against it:


========================
Partial Class vb_FilePage
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

RenderFile()

End Sub

Protected Sub RenderFile()

Dim filename As String
filename = Request.QueryString("fn")

Dim buffer As Byte()

buffer = GetPDFBuffer(filename)

Response.ClearHeaders()
Response.ClearContent()


Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition",
"attachment;filename=YourReport.pdf")

Response.End()

End Sub


End Class
===============================


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

Dominick Baier

this is technically correct - BUT

never implement it like this!

You are taking an arbitrary filename coming over a querystring and use that
to open a file. This is prone to directory traversal attacks.

Before you use the file you should do some input validation

a) check first if the filename is in a list of valid names from your content
directory (File/Directory.* APIs)
b) have a mapping between file ids and actual physical files like /download.aspx?id=5
 
R

Rick

Thanks Steven!
That works the problem seems to be in:

Response.AddHeader("content-disposition",
"attachment;filename=YourReport.pdf")

If it is set to "inline" that's causing the problem, but is there a way to
turn off the save as prompt so the file automatically opens?
 
R

Rick

I'm not passing the file name in, I'm passing an ID, that gets passed to a
web service and that looks up the file in the database.
 
R

Rick

In addition to this question, is there a way to stream the file to a
separate tab if the user has IE 7 installed?
 
S

Steven Cheng[MSFT]

Thanks for your reply Rick,

For your two new questions, I'm afraid they're both quite specific to the
client side setting:


If it is set to "inline" that's causing the problem, but is there a way to
turn off the save as prompt so the file automatically opens?
===================
the client machine's explore setting will affect the file's open
behavior(dialog confirm)

Tools--->Folder options---> file types



In addition to this question, is there a way to stream the file to a
separate tab if the user has IE 7 installed?
=======================
Tab is a pure client-side browser feature, it depend on whether user has
configured to open new page/url in tab or new window... Server side has
no control over this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng[MSFT]

Hi Rick,

Have you got any progress or still any questions? Please feel free to let
me know if there is anything else we can help.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rick

Thanks Steven! I have it working. Maybe you can tell me how to do this
though, when the file is streamed to the browser it doesn't always show on
top of the calling web page, is there a way to force the streamed
file(attachement) to the top?
 
S

Steven Cheng[MSFT]

Thanks for your reply Rick,

As for the new question about "showing PDF doc at the top of the web page",
I'm not quite sure on this, as far as I know, when opening a PDF document
in IE browser(v 7), it will be displayed under the tab bar. Doesn't the pdf
you render out display like this?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rick

I'm sorry Steven, I should have explained this better. I am streaming the
file to the browser using Response.AddHeader("content-disposition",
"attachment;filename=YourReport.pdf") and the other code explained in the
earlier posts. This opens the file in a new instance of IE(v.7), but on my
machine the new instance doesn't always open on top(have focus) of the
calling page.Sometimes it will open behind the calling page, sometimes it
will look like it opens on top but the calling page shows through it, like
it's transparent and to get it on top or get focus on the file page you have
to click on the file page twice to bring the focus to it.
 
S

Steven Cheng[MSFT]

Hi Rick,

Thanks for your reply.

For the further question about controlling the new windows's location and
top level, I think it is still controlled by the client script
implementation(such as the window.open function) of webbrowser. Here is a
thread discussing on control the opened browser's position:

http://www.thescripts.com/forum/thread89595.html

and the IE's window.open method reference:

#open Method
http://msdn2.microsoft.com/en-us/library/ms536651.aspx

However, seems there is no means to control the z-order of the window.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top