Solved

output PDF file using memory stream or response stream

Posted on 2007-11-18
3
15,549 Views
Last Modified: 2011-04-13
I have a linkbutton click event that dyanmically calls a javascript function to open a new window and also calls a method that uses the filestream object to write a PDF document retrieved as a byte array to the disk.

Here is the code, which all works correctly.

private void retrieve()
{

// Invoke retrieve call and save results in an array of SObjects

sObject[] records = sfdc.retrieve("Body", "Document", new String[] { "01570000000GXCZ" });
// Iterate through the results

for (int i = 0; i < records.Length; i++)
{

Document document = (Document)records[i];
// Get the document properties

string rootpath = MapPath("~");
FileStream file = new FileStream(rootpath + "/icg/sysreq.pdf", FileMode.Create);

BinaryWriter bw = new BinaryWriter(file);
bw.Write(document.Body);

bw.Flush();

bw.Close();

file.Close();

}

}

protected void LinkButton1_Click(object sender, EventArgs e)
{

login();

retrieve();

string rootpath = MapPath("~");
StringBuilder sb = new StringBuilder();

sb.Append("<script>");
sb.Append("window.open('sysreq.pdf', '', '');");

sb.Append("</script>");
Page.RegisterStartupScript("test", sb.ToString());

}

What I want to acccomplish is instead of writing the file to disk, I want to write it to memory using a memorystream or the response stream and open the file from memory as needed.

Can anyone help on how to do to this?  The file is a PDF document and is stored as base64 but, it returns the file body as a byte array.


Here is how I tried to use the memory stream, but I can't figure out how to output the PDF file into the new window:

private void retrieve()
{

// Invoke retrieve call and save results in an array of SObjects

sObject[] records = sfdc.retrieve("Body", "Document", new String[] { "01570000000GXCZ" });
// Iterate through the results

for (int i = 0; i < records.Length; i++)
{

Document document = (Document)records[i];
// Get the document properties

//string rootpath = MapPath("~");

//FileStream file = new FileStream(rootpath + "/icg/sysreq.pdf", FileMode.Create);

//BinaryWriter bw = new BinaryWriter(file);

//bw.Write(document.Body);

//bw.Flush();

//bw.Close();

//file.Close();

MemoryStream ms = new MemoryStream();StreamWriter sw = new StreamWriter(ms);
sw.Write(document.Body);

sw.Flush();

sw.Close();

ms.Close();

}

}

protected void LinkButton1_Click(object sender, EventArgs e)
{

login();

retrieve();

string rootpath = MapPath("~");
StringBuilder sb = new StringBuilder();

sb.Append("<script>");
sb.Append("window.open('sysreq.pdf', '', '');");

sb.Append("</script>");Page.RegisterStartupScript("test", sb.ToString());
}



Here is how I tried to use the response stream, but again, after writing the PDF to the response stream, how can I read the stream into a new window?

When the LinikButton_click method is executed the PDF opens in the same browser window.  How can I output the PDF into the new window using the dynamic javascript?

private void retrieve()
{

// Invoke retrieve call and save results in an array of SObjects

sObject[] records = sfdc.retrieve("Body", "Document", new String[] { "01570000000GXCZ" });
// Iterate through the results

for (int i = 0; i < records.Length; i++)
{

Document document = (Document)records[i];


Response.ClearContent(); Response.ContentType = "application/pdf";
Response.BinaryWrite(document.Body);

Response.Flush();

Response.Close();

}

}

protected void LinkButton1_Click(object sender, EventArgs e)
{

login();

retrieve();

string rootpath = MapPath("~");
StringBuilder sb = new StringBuilder();

sb.Append("<script>");
sb.Append("window.open('sysreq.pdf', '', '');");

sb.Append("</script>");
Page.RegisterStartupScript("test", sb.ToString());

}


Thanks for any help.
0
Comment
Question by:-Dman100-
3 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
Comment Utility
Specify _blank as target when you open the window:

sb.Append("window.open('thePageThatLoadsThPdf.aspx', '_blank', '');");

In the page that you open in the window, you put the code that reads the pdf data and outputs it to the response stream. This has to be a separate page. When you write to the response stream, that is the only thing that you can do in that page. The response only contains the http header and the pdf data.

(It's possible to use the same page, making the page act as two completely separate pages depeding on the data that you send to it, but there is no real benefit in that.)
0
 

Author Comment

by:-Dman100-
Comment Utility
Hi GreenGhost,

That got it working just as I needed.

Thanks for the help.  I appreciate it.
Regards.
0
 

Expert Comment

by:jtyce
Comment Utility
This looks like some fabulous code and I could really use it for what I am trying to do with an Excel document. However, what is the sfdc in " sfdc.retrieve"?
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now