Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

output PDF file using memory stream or response stream

Posted on 2007-11-18
3
Medium Priority
?
15,653 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-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

688 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