覆盖页面渲染/StreamReader 大小限制?

发布于 2024-08-18 14:45:48 字数 901 浏览 4 评论 0原文

因此,我正在尝试从标准 ASP.Net 页面中获取 HTML。我已经重写了 Render 方法,如下所示:

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();
    }
}

这对于小于 8.00 KB(8,200 字节)的文件非常有效。如果文件大于该值,则末尾的所有文本都会被截断。

有人有建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喵星人汪星人 2024-08-25 14:45:48

也许,调用 base.Render() 后的 textWriter.Flush() 会拯救你的一天。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文