I am trying to generate a html file using stream writer class of c# in ASP>net?

i am writing the html code inside a text box and when i clicked the save button it will produce the html file.
But it is showing an run time error…

Missing <!doctype html> before <html>?

The “For more information see” that goes to MSDN doesn’t mention that

It was a stab in the dark - I’m not familiar with the ASP.NET environment (yet, about to change though). The doctype declaration is a (not quite) required directive in html5 though, so try including it and seeing if it fixes the problem?

More info on doctype, there are probably better/more definitive references including the html5 spec if you prefer reading that type of thing. :slight_smile:

/d

1 Like

I used that but still it is not working.

You need to read the page @Mittineague replied with. You are running into ASP.NET’s default request validation, which is there to help prevent XSS attacks. You can disable it, but be warned, you may be opening yourself up to attacks.

Yeah , i read it and some how i solved it but here i got another problem.

public partial class Tool : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void ExportHtml()
        {

            {
                using (SaveFileDialog sfd = new SaveFileDialog() { Title = "Save The HTML File", Filter = "Web Page|.html", ValidateNames = true })
                {
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        string htmlContentText = htmlContent.Text;
                        using (StreamWriter sw = new StreamWriter(sfd.FileName))
                        {
                            sw.WriteLineAsync(htmlContentText);
                            MessageBox.Show("File Saved");
                        }
                    }
                }
            }
        }
        protected void ecportHtml_Click(object sender, EventArgs e)
        {
            Thread thread = new System.Threading.Thread(new ThreadStart(ExportHtml));
            thread.Start();
        }
    }
}

And now my error is -
Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.

thanks for that since i learned a lot from it
Yeah , i read it and some how i solved it but here i got another problem.

public partial class Tool : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void ExportHtml()
        {

            {
                using (SaveFileDialog sfd = new SaveFileDialog() { Title = "Save The HTML File", Filter = "Web Page|.html", ValidateNames = true })
                {
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        string htmlContentText = htmlContent.Text;
                        using (StreamWriter sw = new StreamWriter(sfd.FileName))
                        {
                            sw.WriteLineAsync(htmlContentText);
                            MessageBox.Show("File Saved");
                        }
                    }
                }
            }
        }
        protected void ecportHtml_Click(object sender, EventArgs e)
        {
            Thread thread = new System.Threading.Thread(new ThreadStart(ExportHtml));
            thread.Start();
        }
    }
}

And now my error is -
Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.

Can you explain what the program is supposed to do? My guess is that you want the client browser to show a textbox and then the user will enter HTML into it. Then the ASP.Net program will save the HTML to a file. Is that what you are trying to do?

I am nearly certain that the SaveFileDialog will execute in the server, not the client. If you want the HTML to be saved in the client’s computer then it won’t work. Perhaps you could save the HTML in a temporary file in the server then download that but you would need to have a way to clean up the files in the server. I think the best alternative is to use some JavaScript in the client page to do the saving of the file.

How to save a file with java script?

If you mean JavaScript, the question is not how to, but can you. The answer is no.

You can write files to your computer in various ways, but a website can not.

A website can provide a file for download though.

Yep, that’s the usual way of “saving” a file.

Good article on this here: Creating a downloadable file in the browser

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.