Save ASPX page on client programmatically

Discussion in 'ASP .Net' started by =?Utf-8?B?RnJlZU5FYXN5?=, Mar 2, 2007.

  1. Hello,

    the scenario:

    There's an ASPX page which shows some text and has three buttons at the
    bottom: Save, Print and Close. Print and close is done by javascript. But how
    can I save the page on the client's computer? He/she could do this using the
    browser (file/save), but I need to have it done by pressing the pushbutton.

    In my serverside code I get the button-click-event, I also know how to get
    data to the client (including file-save-dialog), but where can I get the
    page's content?

    The download code could look like this (with ??? being actually my question):

    Response.AddHeader("Content-Disposition", "attachment;
    filename=Contract.html");
    Response.AddHeader("Content-Length", ???.Length.ToString());
    Response.ContentType = "application/octet-stream";
    Response.Write(???);
    Response.End();

    Maybe I'm completely wrong and there is an other better way to do it. Please
    help!
     
    =?Utf-8?B?RnJlZU5FYXN5?=, Mar 2, 2007
    #1
    1. Advertising

  2. =?Utf-8?B?RnJlZU5FYXN5?=

    Aidy Guest

    Wrong newsgroup, but you can probably usee something like document.innerHTML
    or document.innerText or something.

    The problem with that is that it won't include images or anything included
    etc. IE's client save is actually doing a lot of work.

    "FreeNEasy" <> wrote in message
    news:...
    > Hello,
    >
    > the scenario:
    >
    > There's an ASPX page which shows some text and has three buttons at the
    > bottom: Save, Print and Close. Print and close is done by javascript. But
    > how
    > can I save the page on the client's computer? He/she could do this using
    > the
    > browser (file/save), but I need to have it done by pressing the
    > pushbutton.
    >
    > In my serverside code I get the button-click-event, I also know how to get
    > data to the client (including file-save-dialog), but where can I get the
    > page's content?
    >
    > The download code could look like this (with ??? being actually my
    > question):
    >
    > Response.AddHeader("Content-Disposition", "attachment;
    > filename=Contract.html");
    > Response.AddHeader("Content-Length", ???.Length.ToString());
    > Response.ContentType = "application/octet-stream";
    > Response.Write(???);
    > Response.End();
    >
    > Maybe I'm completely wrong and there is an other better way to do it.
    > Please
    > help!
     
    Aidy, Mar 2, 2007
    #2
    1. Advertising

  3. =?Utf-8?B?RnJlZU5FYXN5?=

    Norman Yuan Guest

    It depends on what do you want user to save. Since it is web application,
    all user gets is a page rendered in a browser, according to HTML tags. If
    you want user to save the page as it is, then the browser's "Save" menu is
    what it means, why try to invent the wheel again. If what you want user to
    save is the data displayed on the page, but in different format (say, PDF),
    you need then to re-organise your data in wanted format and send to browser
    as download stream. Or because of the current page has too many decorations,
    you want to save a clean printable version, then you can simply make a plain
    printable version of the page, to let user still be able to save with
    standard browser "Save" command.

    I do not see the point when user can save the page easily, while you still
    want him to save the exactly same page as download via a "Save" button.


    "FreeNEasy" <> wrote in message
    news:...
    > Hello,
    >
    > the scenario:
    >
    > There's an ASPX page which shows some text and has three buttons at the
    > bottom: Save, Print and Close. Print and close is done by javascript. But
    > how
    > can I save the page on the client's computer? He/she could do this using
    > the
    > browser (file/save), but I need to have it done by pressing the
    > pushbutton.
    >
    > In my serverside code I get the button-click-event, I also know how to get
    > data to the client (including file-save-dialog), but where can I get the
    > page's content?
    >
    > The download code could look like this (with ??? being actually my
    > question):
    >
    > Response.AddHeader("Content-Disposition", "attachment;
    > filename=Contract.html");
    > Response.AddHeader("Content-Length", ???.Length.ToString());
    > Response.ContentType = "application/octet-stream";
    > Response.Write(???);
    > Response.End();
    >
    > Maybe I'm completely wrong and there is an other better way to do it.
    > Please
    > help!
     
    Norman Yuan, Mar 2, 2007
    #3
  4. @Norman:

    You are right, the browser's save function would be fine for this. There are
    two reasons why I need to have a button on the page for it:

    reason 1: Without the intention to offend someone: there ARE users out there
    who don't know THAT and HOW they can save a page displayed in their browser.
    Since the document is a contract document and it is important that the user
    can save it, (and does save it) a clearly visible save-button is the more
    secure way to have the document saved.

    reason 2: I am programming this for a customer and he WANTS the save-button.

    Besides, if I would like to modify the content for formatting etc. I first
    need to have the content. The ASPX page ist made of a form with text and
    fields, how can I get the rendered content, I can't find it.

    "Norman Yuan" wrote:

    > It depends on what do you want user to save. Since it is web application,
    > all user gets is a page rendered in a browser, according to HTML tags. If
    > you want user to save the page as it is, then the browser's "Save" menu is
    > what it means, why try to invent the wheel again. If what you want user to
    > save is the data displayed on the page, but in different format (say, PDF),
    > you need then to re-organise your data in wanted format and send to browser
    > as download stream. Or because of the current page has too many decorations,
    > you want to save a clean printable version, then you can simply make a plain
    > printable version of the page, to let user still be able to save with
    > standard browser "Save" command.
    >
    > I do not see the point when user can save the page easily, while you still
    > want him to save the exactly same page as download via a "Save" button.
    >
    >
    > "FreeNEasy" <> wrote in message
    > news:...
    > > Hello,
    > >
    > > the scenario:
    > >
    > > There's an ASPX page which shows some text and has three buttons at the
    > > bottom: Save, Print and Close. Print and close is done by javascript. But
    > > how
    > > can I save the page on the client's computer? He/she could do this using
    > > the
    > > browser (file/save), but I need to have it done by pressing the
    > > pushbutton.
    > >
    > > In my serverside code I get the button-click-event, I also know how to get
    > > data to the client (including file-save-dialog), but where can I get the
    > > page's content?
    > >
    > > The download code could look like this (with ??? being actually my
    > > question):
    > >
    > > Response.AddHeader("Content-Disposition", "attachment;
    > > filename=Contract.html");
    > > Response.AddHeader("Content-Length", ???.Length.ToString());
    > > Response.ContentType = "application/octet-stream";
    > > Response.Write(???);
    > > Response.End();
    > >
    > > Maybe I'm completely wrong and there is an other better way to do it.
    > > Please
    > > help!

    >
    >
    >
     
    =?Utf-8?B?RnJlZU5FYXN5?=, Mar 2, 2007
    #4
  5. @Aidy:

    -> "document.innerHTML or document.innerText or something"
    What I am lookin for is 'or something'. I can't find an object which holds
    the page's rendered content. I don't need images since there are no images on
    the page, only formatted text (and three buttons).

    -> Wrong newsgroup
    Why, and which would be right?

    "Aidy" wrote:

    > Wrong newsgroup, but you can probably usee something like document.innerHTML
    > or document.innerText or something.
    >
    > The problem with that is that it won't include images or anything included
    > etc. IE's client save is actually doing a lot of work.
    >
    > "FreeNEasy" <> wrote in message
    > news:...
    > > Hello,
    > >
    > > the scenario:
    > >
    > > There's an ASPX page which shows some text and has three buttons at the
    > > bottom: Save, Print and Close. Print and close is done by javascript. But
    > > how
    > > can I save the page on the client's computer? He/she could do this using
    > > the
    > > browser (file/save), but I need to have it done by pressing the
    > > pushbutton.
    > >
    > > In my serverside code I get the button-click-event, I also know how to get
    > > data to the client (including file-save-dialog), but where can I get the
    > > page's content?
    > >
    > > The download code could look like this (with ??? being actually my
    > > question):
    > >
    > > Response.AddHeader("Content-Disposition", "attachment;
    > > filename=Contract.html");
    > > Response.AddHeader("Content-Length", ???.Length.ToString());
    > > Response.ContentType = "application/octet-stream";
    > > Response.Write(???);
    > > Response.End();
    > >
    > > Maybe I'm completely wrong and there is an other better way to do it.
    > > Please
    > > help!

    >
    >
    >
     
    =?Utf-8?B?RnJlZU5FYXN5?=, Mar 2, 2007
    #5
  6. =?Utf-8?B?RnJlZU5FYXN5?=

    Ornette Guest

    Maybe you can call the SaveAs of the browser. For exemple IE :
    http://www.codefoot.com/javascript/script_invoke_saveas.html
    Or send a stream (pdf for exemple).


    "FreeNEasy" <> a écrit dans le message de
    news:...
    > Hello,
    >
    > the scenario:
    >
    > There's an ASPX page which shows some text and has three buttons at the
    > bottom: Save, Print and Close. Print and close is done by javascript. But
    > how
    > can I save the page on the client's computer? He/she could do this using
    > the
    > browser (file/save), but I need to have it done by pressing the
    > pushbutton.
    >
    > In my serverside code I get the button-click-event, I also know how to get
    > data to the client (including file-save-dialog), but where can I get the
    > page's content?
    >
    > The download code could look like this (with ??? being actually my
    > question):
    >
    > Response.AddHeader("Content-Disposition", "attachment;
    > filename=Contract.html");
    > Response.AddHeader("Content-Length", ???.Length.ToString());
    > Response.ContentType = "application/octet-stream";
    > Response.Write(???);
    > Response.End();
    >
    > Maybe I'm completely wrong and there is an other better way to do it.
    > Please
    > help!
     
    Ornette, Mar 2, 2007
    #6
  7. Hi FreeNEasy,

    As some other members have provided some suggestion about retrieving page's
    content, for example, use javascript DOM api to get the innerHTML of page
    body.

    #In Javascript I Want To Get The Page Source, And Store It In A Variable?
    http://qa.techinterviews.com/q/20060731161006AAQncKe

    The problem here is save the content to a file on client-side file system.
    Generally, browser based web page application has natural security
    restrictions on accessing client user's local resource(such as file system,
    network, registry ....). Therefore, the default client-script code can not
    create , modify or delete any file on the client system. To do this, you
    need to either use windows script or activeX component which can leverage
    strong code to do those privilege operations, however, this require the
    client user to allow such strong code be executed in webbrowser. For your
    scenario, I think this seems not quite convenient since you need to ask
    every client user to adjust their client browser security setting.

    Do you think it possible that you simply use a button to popup a help page
    that demonstrate the steps for saving the a page in browser?

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead



    ==================================================

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.



    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.

    ==================================================



    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Mar 5, 2007
    #7
  8. =?Utf-8?B?RnJlZU5FYXN5?=

    Ornette Guest

    Hello,

    Yes, saving to the client's computer directly is quite impossible as it runs
    in a sandbox.
    I think the only solution is sending a stream (document like pdf or
    something else) and the user can clic "save as" or "open".

    Maybe you can change the behaviour if you users are authenticated. You can
    send it by email ?

    Or you can format it in a printable version and put a "print" button (most
    of the websites do this + email).

    Ornette

    "Steven Cheng[MSFT]" <> a écrit dans le message
    de news:...
    > Hi FreeNEasy,
    >
    > As some other members have provided some suggestion about retrieving
    > page's
    > content, for example, use javascript DOM api to get the innerHTML of page
    > body.
    >
    > #In Javascript I Want To Get The Page Source, And Store It In A Variable?
    > http://qa.techinterviews.com/q/20060731161006AAQncKe
    >
    > The problem here is save the content to a file on client-side file system.
    > Generally, browser based web page application has natural security
    > restrictions on accessing client user's local resource(such as file
    > system,
    > network, registry ....). Therefore, the default client-script code can not
    > create , modify or delete any file on the client system. To do this, you
    > need to either use windows script or activeX component which can leverage
    > strong code to do those privilege operations, however, this require the
    > client user to allow such strong code be executed in webbrowser. For your
    > scenario, I think this seems not quite convenient since you need to ask
    > every client user to adjust their client browser security setting.
    >
    > Do you think it possible that you simply use a button to popup a help page
    > that demonstrate the steps for saving the a page in browser?
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    >
    > ==================================================
    >
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > ications.
    >
    >
    >
    > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 1 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions or complex
    > project analysis and dump analysis issues. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    >
    > ==================================================
    >
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
     
    Ornette, Mar 5, 2007
    #8
  9. Thanks for your input Ornette,

    I think you've pointed out a good approach here. We can put a button on
    the page which simply postback to server(or redirect to another page) that
    will flush out the entire page content(document) as attachment mode. The
    code logic in the postback event or another page should be as below:

    **write all the document content into response stream

    ** change the response Content type as the proper one(csv or text or ...)

    **add the attachment header such as
    Response.AddHeader("Content-disposition", "attachment;
    filename=FlatPDFForm.fdf");

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Mar 5, 2007
    #9
  10. Thank you Ornette, thank you Steven,

    the approach you suggested has come to my mind too. It's the very best
    solution. Calling browser functions is always difficult because of browser
    differences (IE, Firefox etc.).

    > **write all the document content into response stream

    The only remaining problem I still have is, where/how (server side code) can
    I get the document content? The ASPX page ist made of a form with text and
    fields, how can I get the rendered content, I can't find it.

    > ** change the response Content type as the proper one(csv or text or ...)

    ==>> READY

    > **add the attachment header such as
    > Response.AddHeader("Content-disposition", "attachment;
    > filename=FlatPDFForm.fdf");

    ==>> READY

    Sincerely,
    Michael



    "Steven Cheng[MSFT]" wrote:

    > Thanks for your input Ornette,
    >
    > I think you've pointed out a good approach here. We can put a button on
    > the page which simply postback to server(or redirect to another page) that
    > will flush out the entire page content(document) as attachment mode. The
    > code logic in the postback event or another page should be as below:
    >
    > **write all the document content into response stream
    >
    > ** change the response Content type as the proper one(csv or text or ...)
    >
    > **add the attachment header such as
    > Response.AddHeader("Content-disposition", "attachment;
    > filename=FlatPDFForm.fdf");
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >
    >
    >
     
    =?Utf-8?B?RnJlZU5FYXN5?=, Mar 5, 2007
    #10
  11. Thanks for your reply Michael,

    For your further question below:

    The only remaining problem I still have is, where/how (server side code)
    can
    I get the document content? The ASPX page ist made of a form with text and
    fields, how can I get the rendered content, I can't find it.
    ==================================================

    So far based on my test, I have found the following two approachs:

    1. just use the postback event(button click) in the same page, and add the
    file download header in the postback event. e.g.

    =====in page codebehind===
    protected void btnSavePage_Click(object sender, EventArgs e)
    {
    this.Response.ContentType = "text/html";
    this.Response.AddHeader("Content-Disposition", "attachment;
    filename=output.htm");

    }
    ==============


    2. Or your can also try executing another page and flush the content of
    another page into current page's response by Server.Execute method. e.g.

    =====in another page================
    protected void Page_Load(object sender, EventArgs e)
    {

    Response.ClearContent();

    Server.Execute("~/accesspage.aspx");


    Response.ContentType = "text/html";
    Response.AddHeader("Content-Disposition", "attachment;
    filename=output.htm");


    Response.End();
    }
    =====================

    Hope this also helps some.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Mar 6, 2007
    #11
  12. Thank you Steven,

    this was the answer to my question.

    I already tried approach #1, but made the mistake to add Response.End() at
    the bottom, which resulted in an empty html-page.

    MANY THANKS TO ALL WHO TRIED TO HELP ME.

    Michael



    "Steven Cheng[MSFT]" wrote:

    > Thanks for your reply Michael,
    >
    > For your further question below:
    >
    > The only remaining problem I still have is, where/how (server side code)
    > can
    > I get the document content? The ASPX page ist made of a form with text and
    > fields, how can I get the rendered content, I can't find it.
    > ==================================================
    >
    > So far based on my test, I have found the following two approachs:
    >
    > 1. just use the postback event(button click) in the same page, and add the
    > file download header in the postback event. e.g.
    >
    > =====in page codebehind===
    > protected void btnSavePage_Click(object sender, EventArgs e)
    > {
    > this.Response.ContentType = "text/html";
    > this.Response.AddHeader("Content-Disposition", "attachment;
    > filename=output.htm");
    >
    > }
    > ==============
    >
    >
    > 2. Or your can also try executing another page and flush the content of
    > another page into current page's response by Server.Execute method. e.g.
    >
    > =====in another page================
    > protected void Page_Load(object sender, EventArgs e)
    > {
    >
    > Response.ClearContent();
    >
    > Server.Execute("~/accesspage.aspx");
    >
    >
    > Response.ContentType = "text/html";
    > Response.AddHeader("Content-Disposition", "attachment;
    > filename=output.htm");
    >
    >
    > Response.End();
    > }
    > =====================
    >
    > Hope this also helps some.
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >
    >
    >
     
    =?Utf-8?B?RnJlZU5FYXN5?=, Mar 6, 2007
    #12
  13. I also had to solve this problem and did use Steven's approach. It worked
    great!

    Now here is an interesting twist. The page that gets saved to the client
    contains a several DataGrids components. The data displayed in the grids is
    filtered by the contents of a separate drop down box. This allows the
    client to "download" one page a time.

    I now need to replace the drop down by a list box that support
    multi-selects. When the client user clicks on the "download" button, I need
    to generate several versions of the page, each displaying the data related
    to one of the selections in the list box.

    How do I do this?

    I first tried to extend the approach given here ahd have the page render
    itself several times. Each time it renders itself with new data, I would
    capture the stream into a file on the server. Once we are done with all the
    renderings, I would then zip up the files and send them to the client. The
    question is, how can I force the page to render itself several times?

    My next idea is to use a session variable to store all the selections from
    the list box. I then create another page whose sole purpose is to redirect
    me to my original page. In my original page, every time it loads, I check
    the session variable and move on to the next item in the list. Once I
    capture its output, I then redirect the response to the newly created page,
    which will in turn send me back to my original page, etc.

    Any ideas?
     
    Joseph I. Ceasar, May 9, 2007
    #13
    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. Sriram Mallajyosula

    How do I post data from aspx page to another aspx page.

    Sriram Mallajyosula, Nov 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,028
    Steve C. Orr [MVP, MCSD]
    Nov 5, 2003
  2. John M
    Replies:
    1
    Views:
    1,261
    Kumar Reddi
    May 29, 2005
  3. subhadip
    Replies:
    0
    Views:
    664
    subhadip
    Mar 28, 2007
  4. Arnaud PICHERY

    Adding a Register directive to an aspx page programmatically

    Arnaud PICHERY, Nov 16, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    404
    Teemu Keiski
    Nov 18, 2003
  5. Arnaud PICHERY
    Replies:
    0
    Views:
    166
    Arnaud PICHERY
    Nov 14, 2003
Loading...

Share This Page