PDFsharp & MigraDoc Foundation https://forum.pdfsharp.net/ |
|
Issue with accessing stream length https://forum.pdfsharp.net/viewtopic.php?f=2&t=1380 |
Page 1 of 1 |
Author: | andles [ Wed Oct 20, 2010 9:14 am ] |
Post subject: | Issue with accessing stream length |
Hi All, I want to set the stream length for an RTF doc being output over the web, but due to the way the stream is handled in the render method (I think?) I am forced to omit this. Is this a bug?? // Create a renderer for the MigraDoc document. RtfDocumentRenderer rtfRenderer = new RtfDocumentRenderer(); // Send to browser MemoryStream stream = new MemoryStream(); // Do these now, or will have issues with closed stream! Response.Clear(); Response.ContentType = "application/msword"; // text/rtf? // Associate the MigraDoc document with a renderer rtfRenderer.Render(migraDoc, stream, null); PROBLEM HERE! ACCESSING STREAM LENGTH CAUSES 'cannot access closed stream' - COMMENTING IT OUT SOLVES THE PROBLEM, BUT IDEALLY THE BROWSER WANTS TO KNOW THE FILE SIZE. // Response.AddHeader("content-length", stream.Length.ToString()); Response.BinaryWrite(stream.ToArray()); Response.Flush(); stream.Close(); Response.End(); Any thoughts, ideas on this?? Cheers, Andles... |
Author: | Thomas Hoevel [ Wed Oct 20, 2010 9:26 am ] |
Post subject: | Re: Issue with accessing stream length |
Something like this: Code: var bytes = stream.ToArray(); Response.AddHeader("content-length", bytes.Length.ToString()); Response.BinaryWrite(bytes); (Just an idea and completely untested) |
Author: | andles [ Wed Oct 20, 2010 10:44 am ] |
Post subject: | Re: Issue with accessing stream length |
Excellent - that did the trick! I assumed that the render method was closing the stream - but possibly it is the ToArray() call that alters the state of the stream? Either way - many thanks, problem solved. Here's my final code in case others encounter this when using memory stream and/or outputting RTF to a web browser: private void RenderAsRTF(Document migraDoc) { // Create a renderer for the MigraDoc document. RtfDocumentRenderer rtfRenderer = new RtfDocumentRenderer(); // Send to browser MemoryStream stream = new MemoryStream(); // Do these now, or will have issues with closed stream! Response.Clear(); Response.ContentType = "application/msword"; // text/rtf? // Associate the MigraDoc document with a renderer rtfRenderer.Render(migraDoc, stream, null); byte[] bytes = stream.ToArray(); Response.AddHeader("content-length", bytes.Length.ToString()); Response.BinaryWrite(bytes); Response.Flush(); stream.Close(); Response.End(); } |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |