Solved

output PDF file using memory stream or response stream

Posted on 2007-11-18
3
15,591 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 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.net Moving Visual Studio 2015 project to 2017 2 68
Need help with a query 14 53
CSS for Popup in ASP.NET 4 48
dropdownlist in asp.net vb. 3 21
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Suggested Courses

734 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