Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Render of a page / StreamReader size limit?

Tags:

So, I'm playing around with getting the HTML out of a standard ASP.Net Page. I've overridden the Render method, as so:

protected override void Render(HtmlTextWriter writer)
{
    MemoryStream memoryStream = new MemoryStream();

    try
    {
        using (StreamWriter streamWriter = new StreamWriter(memoryStream))
        {
            var textWriter = new HtmlTextWriter(streamWriter);
            base.Render(textWriter);
            memoryStream.Position = 0;
            using (StreamReader reader = new StreamReader(memoryStream))
            {
                var text = reader.ReadToEnd();
                Response.Write(text);
                reader.Close();
            }
        }
    }
    catch(ObjectDisposedException)
    {
        // The stream writer is already disposed?
    }
    finally
    {
        memoryStream.Dispose();
    }
}

This works great on files that are less than 8.00 KB (8,200 bytes). If the file is larger than that, any text at the end is being cut off.

Does anyone have an suggestions?

like image 313
Matt Grande Avatar asked Jan 18 '10 15:01

Matt Grande


1 Answers

Maybe, textWriter.Flush() after a call to base.Render() will save your day.

like image 53
Anton Gogolev Avatar answered Oct 11 '22 15:10

Anton Gogolev