Solved

output PDF file using memory stream or response stream

Posted on 2007-11-18
3
15,552 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
ID: 20307471
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-
ID: 20308162
Hi GreenGhost,

That got it working just as I needed.

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

Expert Comment

by:jtyce
ID: 22273133
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

895 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

13 Experts available now in Live!

Get 1:1 Help Now