Send stream but not end app?

D

dgk

This code sends a PDF but ends the application. How can I send the
stream but keep the session running? Thanks.

Dim ms As New system.io.memorystream
Dim document As New iTextSharp.text.Document
Dim writer As iTextSharp.text.pdf.PdfWriter =
iTextSharp.text.pdf.PdfWriter.GetInstance(document, ms)
document.open()
document.Add(New iTextSharp.text.Paragraph("Hello World"))
document.close()
Response.ClearContent()
Response.ContentType = "application/pdf"
Response.BinaryWrite(ms.GetBuffer)
Response.End()
 
G

Guest

It doesn't "end the application", it just ends the Response output stream for
that page. Have you tried a server.transfer or Response.Redirect to a
"Success page" instead?
Peter
 
D

dgk

It doesn't "end the application", it just ends the Response output stream for
that page. Have you tried a server.transfer or Response.Redirect to a
"Success page" instead?
Peter

I tried response.redirect, that cancelled the pdf. Server.Transfer
also cancels the pdf. I seem to have a choice of not sending the pdf,
and returning to default.aspx, or sending the pdf and ending the app.
By ending the app, I mean that no browser window is open and Visual
Studio drops out of run and back into edit:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim ms As New memorystream
Dim document As New iTextSharp.text.Document
Dim writer As iTextSharp.text.pdf.PdfWriter =
iTextSharp.text.pdf.PdfWriter.GetInstance(document, ms)
document.open()
document.Add(New iTextSharp.text.Paragraph("Hello World"))
document.close()
Response.ClearContent()
Response.ContentType = "application/pdf"
Response.BinaryWrite(ms.GetBuffer)
Response.End()
'Response.Redirect("default.aspx")
Server.Transfer("default.aspx")
End Sub

The browser opens the PDF, and there is no way to get back to the
page.
 
D

dgk

I tried response.redirect, that cancelled the pdf. Server.Transfer
also cancels the pdf. I seem to have a choice of not sending the pdf,
and returning to default.aspx, or sending the pdf and ending the app.
By ending the app, I mean that no browser window is open and Visual
Studio drops out of run and back into edit:

Ah, got it. The problem was that it worked ok if the user saved the
pdf and then opened it; that opened the PDF in a new window. However
just choosing to open it without saving it used the same browser
session, without even a back button, so closing the PDF closed the
session.

However, adding this line:

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

causes the pdf to open in a new browser window even on the original
open. The only problem is a slight flash of a window opening, which is
fine by me. Thanks to a post by "Random" the final code then:

With Response
.Buffer = True
.ClearContent()
.ClearHeaders()
.ContentType = "application/pdf"
.AddHeader("content-disposition", "attachment;
filename=file.pdf")
.BinaryWrite(ms.GetBuffer) ' this is the memory stream of bytes
containing the pdf
.End()
End With
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top