Save ASPX page on client programmatically

G

Guest

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!
 
A

Aidy

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.
 
N

Norman Yuan

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.
 
G

Guest

@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.
 
G

Guest

@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?
 
S

Steven Cheng[MSFT]

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.
 
O

Ornette

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
 
S

Steven Cheng[MSFT]

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.
 
G

Guest

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
 
S

Steven Cheng[MSFT]

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.
 
G

Guest

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
 
J

Joseph I. Ceasar

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top