Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

output PDF file using memory stream or response stream

Posted on 2007-11-18
3
Medium Priority
?
15,671 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

972 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