Writing the asp.net to a file

Discussion in 'ASP .Net' started by =?Utf-8?B?SWRv?=, Sep 5, 2006.

  1. Is there an option to write the viewed page to a file on the server?
    I have an asp.net page that produce a report according to a user
    information. I want to add a button that when clicked the page will be saved
    to a file on the server. Is that possible?
    =?Utf-8?B?SWRv?=, Sep 5, 2006
    #1
    1. Advertising

  2. Hi,

    Ido wrote:
    > Is there an option to write the viewed page to a file on the server?
    > I have an asp.net page that produce a report according to a user
    > information. I want to add a button that when clicked the page will be saved
    > to a file on the server. Is that possible?


    Yes, it is possible. The idea is to use a self-created HtmlTextWriter
    (which is just a specialized TextWriter), and to pass this writer to all
    the controls.

    Something like that:

    // In Page.Render

    if ( Request.QueryString != null
    && Request.QueryString[ "static" ] == "1" )
    {
    this.RenderStatic( this.Request.ContentEncoding );

    // Send a basic response
    writer.WriteLine( "Static file saved" );
    return;
    }



    // With:

    public void RenderStatic( Encoding oEncoding )
    {
    HtmlTextWriter writer = null;

    try
    {
    writer = new HtmlTextWriter( @"c:\temp\report.html" );

    foreach ( Control child in this.Controls )
    {
    child.RenderControl( writer );
    }
    }
    catch ( Exception ex )
    {
    throw ex;
    }
    finally
    {
    if ( writer != null )
    {
    writer.Close();
    }
    }
    }

    A few notes:

    - The process is triggered by a URL like this:
    http://www.domain.com/page.aspx?static=1

    - If the Render method directly writes HTML as text to the
    HtmlTextWriter, then you must also write this HTML as text to your
    "static" writer.

    - Once the controls have been rendered to a writer, they cannot be
    rendered a second time. This is why I send back a basic response to the
    client instead of attempting to render the normal page. If you don't
    want to display this basic text in the browser, you can use AJAX
    (XmlHttpRequest) to send the request to the URL above, and ignore the
    answer.

    Feel free to ask if something is not clear.

    Greetings,
    Laurent
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    Private/Malaysia: http://mypage.bluewin.ch/lbugnion
    Support children in Calcutta: http://www.calcutta-espoir.ch
    Laurent Bugnion, Sep 5, 2006
    #2
    1. Advertising

  3. Its working but I have a minor problem. All the controls in the page lost
    their style attributes (background color,font size ext').

    "Laurent Bugnion" wrote:

    > Hi,
    >
    > Ido wrote:
    > > Is there an option to write the viewed page to a file on the server?
    > > I have an asp.net page that produce a report according to a user
    > > information. I want to add a button that when clicked the page will be saved
    > > to a file on the server. Is that possible?

    >
    > Yes, it is possible. The idea is to use a self-created HtmlTextWriter
    > (which is just a specialized TextWriter), and to pass this writer to all
    > the controls.
    >
    > Something like that:
    >
    > // In Page.Render
    >
    > if ( Request.QueryString != null
    > && Request.QueryString[ "static" ] == "1" )
    > {
    > this.RenderStatic( this.Request.ContentEncoding );
    >
    > // Send a basic response
    > writer.WriteLine( "Static file saved" );
    > return;
    > }
    >
    >
    >
    > // With:
    >
    > public void RenderStatic( Encoding oEncoding )
    > {
    > HtmlTextWriter writer = null;
    >
    > try
    > {
    > writer = new HtmlTextWriter( @"c:\temp\report.html" );
    >
    > foreach ( Control child in this.Controls )
    > {
    > child.RenderControl( writer );
    > }
    > }
    > catch ( Exception ex )
    > {
    > throw ex;
    > }
    > finally
    > {
    > if ( writer != null )
    > {
    > writer.Close();
    > }
    > }
    > }
    >
    > A few notes:
    >
    > - The process is triggered by a URL like this:
    > http://www.domain.com/page.aspx?static=1
    >
    > - If the Render method directly writes HTML as text to the
    > HtmlTextWriter, then you must also write this HTML as text to your
    > "static" writer.
    >
    > - Once the controls have been rendered to a writer, they cannot be
    > rendered a second time. This is why I send back a basic response to the
    > client instead of attempting to render the normal page. If you don't
    > want to display this basic text in the browser, you can use AJAX
    > (XmlHttpRequest) to send the request to the URL above, and ignore the
    > answer.
    >
    > Feel free to ask if something is not clear.
    >
    > Greetings,
    > Laurent
    > --
    > Laurent Bugnion, GalaSoft
    > Software engineering: http://www.galasoft-LB.ch
    > Private/Malaysia: http://mypage.bluewin.ch/lbugnion
    > Support children in Calcutta: http://www.calcutta-espoir.ch
    >
    =?Utf-8?B?SWRv?=, Sep 5, 2006
    #3
  4. Hi,

    Ido wrote:
    > Its working but I have a minor problem. All the controls in the page lost
    > their style attributes (background color,font size ext').


    How do you define the styles? in external CSS files? If yes, you must be
    careful that the paths are correct, for example that the static HTML
    file is saved in the same folder as the ASPX file (if that's possible).

    HTH,
    Laurent
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
    Laurent Bugnion, Sep 5, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Hamilton

    writing to an Excel file using ASP.Net

    Jeff Hamilton, Nov 24, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    2,813
    Alvin Bruney
    Nov 26, 2003
  2. Curt_C [MVP]
    Replies:
    3
    Views:
    542
    Charlie@CBFC
    Feb 2, 2004
  3. HNguyen
    Replies:
    4
    Views:
    2,387
    HNguyen
    Dec 21, 2004
  4. Phoenix
    Replies:
    0
    Views:
    390
    Phoenix
    Jan 27, 2005
  5. icedragon
    Replies:
    0
    Views:
    637
    icedragon
    Jul 13, 2010
Loading...

Share This Page