Link to home
Start Free TrialLog in
Avatar of simlox
simloxFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Create a html page dynamically and save it

The thing I'm trying to do is:

- take some user parameters (this is complete)
- create an html page using those parameters (this is complete)
- return the page to the user calling the page as a 'downloadable file'

From the page I have tried using the following:

WebClient Client = new WebClient ();
Client.DownloadFile("http://myhost/myApp/TMtemp.htm", "index.htm");

This throws an access denied exception each time.  Is there a better way to do this that simple pops up the 'save as' dialog?
Avatar of daveamour
daveamour
Flag of United Kingdom of Great Britain and Northern Ireland image

You want the users to download the file and save it rather than just viewing it in a browser?
Avatar of simlox

ASKER

Yes, I want them to download it and save it.
So this is what you want right?

User visits a web page and fills in a form and clicks a button.  The page posts back and some server side code creates an html page on the server.  Somthing then happens to allow the user to save this file to their local system.

Is that right?
You write

WebClient Client = new WebClient ();
Client.DownloadFile("http://myhost/myApp/TMtemp.htm", "index.htm");

But, the first parameter of your method is already a HTML page. Don't you want to do this instead:

WebClient Client = new WebClient ();
Client.DownloadFile("http://myhost/myApp/", "TMtemp.htm");

Hope this helps


-=@gS=-
With that aqsapt aren't we getting mixed up with server and client code.  The above looks like client code to me but it reads asif server side code is needed so I'm confused!
Avatar of simlox

ASKER

daveamour wrote :
>>So this is what you want right?

>>User visits a web page and fills in a form and clicks a button.  The page posts back and some server side code creates an html page on the server.  Somthing then happens to allow the user to save this file to their local system.

>>Is that right?

Yes this is exactly what I want...
The that though when you send the htm file to the browser then the browser will just open and render teh html file as that is it purpose!
Avatar of simlox

ASKER

Will I need to write the html to file first then do a server redirect ( or something else)  to open the 'save as' on the client? Or is there a way I can write direct to the browser and prompt a save as when written?

Sorry if a seem like a noob here, but its been a while since I did some web dev... i forgot how much winforms dev messes you web knowledge :(
If you have an html file and you send it to the browser then the browser will just open it.

If on the other hand you send a zip file then the browser will throw up a Save As dialog.

Why don't you explain what your high level requirements are and maybe that will help us help you better?
Avatar of simlox

ASKER

It's a simple web application that allows users to create a simple webpage based on some input parameters... ie. title, description, keywords and some basic text.

I then want the application to create the file as an 'index.htm' file and return the file back to the user so they can save it and upload it to their own web space.  Returning it as a zip file (which you mentioned) may be the route to go.

I was trying to return it direct to the user in case multiple people were creating an index.htm file at the same time.
Do the users have a registered email address?  If so would emailing them the file be a viable option.

Zipping the file is ok so if you want to go down that route we can look at that.  Are you in control of the server?

Avatar of simlox

ASKER

It's shared hosting space with .net 2.0 on it.. So i would have limited options to change anything on the server itself. I may not always have the email addresses though but this is definitely an option and I could add a mandatory field in for this.

I will be using this as a free utility that anyone can use from my web site.. so i'm not sure if zipping route may well be the easiest option.
Ok got it!

Try this:
        StreamReader reader = new StreamReader(@"P:\My Documents\Visual Studio 2008\WebSites\EEGenericSolution\OmegaLoveContactUs.htm");
 
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment; filename=MyHTMLPage.htm");
        Response.Write(reader.ReadToEnd());
 
        reader.Close();
 
        reader.Dispose();

Open in new window

Avatar of simlox

ASKER

That's almost there.. it just seems to add the file contents (the one i want) together with the current document and send a combination of them both back in the file to save

here is the code taken i modified from the code you provided.

        string path=Server.MapPath(".");
        string fl=path + @"\test.htm";

        StreamReader reader = new StreamReader(fl);
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment; filename=index.htm");
        Response.Write(reader.ReadToEnd());

        reader.Close();
        reader.Dispose();

the fiole i'm openening only contains:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
mypage
</body>
</html>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
mypage
</body>
</html>
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
 
</title></head>
<body>
    <form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMjA0OTM4MTAwNGRkQvjWVPnBLf2BUSjsN8XHqsTIInw=" />
</div>
 
<div>
 
	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgL18MfSDgKM54rGBvVhUcHOvKIzoc2OYsw5qkqJiJfr" />
</div>
    <div>
    
        <input type="submit" name="Button1" value="Button" id="Button1" />
    
    </div>
    </form>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of daveamour
daveamour
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of simlox

ASKER

Excellent.. that works... your a star.. thanks very much for all your help.. it's much appreciated..
Your welcome