How does asp.net generate html pages?

Source: Internet
Author: User

1. you can create complex pages and add document to the js file by using methods that contain js files. the write () method can be used to add content such as the page header and advertisement to all pages.

2. The Index Server of MS Windows2000 can be used to create a full-text search engine for static html files, and asp tutorial. net can be used to obtain search results in DataTable mode. The Index Service of Win2000 cannot find the content of xml files. This search function is very powerful if it includes both database tutorial search and Index search.

3. Save the server load and beg a static html file to save a lot of resources than An aspx file server.

Disadvantages

Train of Thought 2: If the hard-coded method is used, the workload is very heavy and a lot of html code is required. Debugging difficulties. Moreover, html styles that are hard-coded cannot be modified. If a website changes its styles, it must be re-encoded, resulting in a huge workload in the future.

Therefore, the first approach is adopted here.

The sample code is as follows:

 

1.define (template.htm) html template page

01.

02. <title> </title>

03. <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">

04.

05. <body>

06. <table $ htmlformat [0] height = "100%" border = "0" width = "100%" cellpadding = "10" cellspacing = "0" bgcolor = "# eeeeee" style = "border: 1px solid #000000 ">

07. <tr>

08. <td width = "100%" valign = "middle" align = "left">

09. <span style = "color: $ htmlformat [1]; font-size: $ htmlformat [2]"> $ htmlformat [3] </span>

10. </td>

11. </tr>

12. </table>

13. </body>

14. Copy the 2.asp.net tutorial code:


01. // ================== read the html template page to the stringbuilder object ==================== string [] format = new string [4]; // define an array with the same number of htmlyem tags

02. StringBuilder htmltext = new StringBuilder ();

03. try

04 .{

05. using (StreamReader sr = new StreamReader ("path for storing template pages and Page name "))

06 .{

07. String line;

08. while (line = sr. ReadLine ())! = Null)

09 .{

10. htmltext. Append (line );

11 .}

12. sr. Close ();

13 .}

14 .}

15. catch

16 .{

17. Response. Write ("<Script> alert ('file reading error') </Script> ");

18.} // --------------------- assign a value to the tag array ------------ format [0] = "background =" bg.jpg "; // background image

19. format [1] = "#990099"; // font color

20. format [2] = "150px"; // font size

21. format [3] = "<marquee> generated template html page </marquee>"; // text description

22. // ---------- Replace the tag in the htm with the content you want to add

23. for (int I = 0; I <4; I ++)

24 .{

25. htmltext. Replace ("$ htmlformat [" + I + "]", format [I]);

26.} // ================== generate an htm file ======================= try

27 .{

28. using (StreamWriter sw = new StreamWriter ("Storage path and Page name", false, System. Text. Encoding. GetEncoding ("GB2312 ")))

29 .{

30. sw. WriteLine (htmltext );

31. sw. Flush ();

32. sw. Close ();

33.} catch {Response. Write ("The file cocould not be wirte :");}
Copy the code summary:
This method can be used to easily generate html files. The program uses cyclic replacement, so it is very fast for templates that need to replace a large number of elements.

Upload a word to generate an html file location

Using System;
Using System. Collections. Generic;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. IO;
Using Microsoft. Office. Interop. Word;

Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{


}

/// Upload the file and save it as htm
Public string Doc2Html (System. Web. UI. HtmlControls. HtmlInputFile wordFilePath)
{
Microsoft. Office. Interop. Word. ApplicationClass word = new Microsoft. Office. Interop. Word. ApplicationClass ();
Type wordType = word. GetType ();
Microsoft. Office. Interop. Word. Documents docs = word. Documents;

// Open the file
Type docsType = docs. GetType ();

// Upload the file first and parse it into html
String filePath = uploadDoc (wordFilePath );

// Determine whether the file is successfully uploaded
If (filePath = "0 ")
Return "0 ";
// Determine whether it is a word file
If (filePath = "1 ")
Return "1 ";

Object fileName = filePath;

// Open a Word document
Microsoft. Office. Interop. Word. Document doc = (Microsoft. Office. Interop. Word. Document) docsType. InvokeMember ("Open ",
System. Reflection. BindingFlags. InvokeMethod, null, docs, new Object [] {fileName, true, true });

// Convert the format and save it as html
Type docType = doc. GetType ();
String strfileName = fileName. ToString ();

// Get the name of the conversion file, with the same name as the Doc file
Int fullNameIndex = strfileName. LastIndexOf ("");
String filename = strfileName. Substring (fullNameIndex );
String pattern = @ "(.doc )";
Filename = System. Text. RegularExpressions. Regex. Replace (filename, pattern, String. Empty );
 
// The location where the converted html document is saved
String ConfigPath = HttpContext. Current. Server. MapPath ("Html/" + filename + ". html ");
Object saveFileName = ConfigPath;

DocType. InvokeMember ("SaveAs", System. Reflection. BindingFlags. InvokeMethod,
Null, doc, new object [] {saveFileName, Microsoft. Office. Interop. Word. WdSaveFormat. wdFormatFilteredHTML });
// Be sure to close it first. Otherwise, resources cannot be released when you exit Word.
DocType. InvokeMember ("Close", System. Reflection. BindingFlags. InvokeMethod, null, doc, null );
// Exit Word
WordType. InvokeMember ("Quit", System. Reflection. BindingFlags. InvokeMethod, null, word, null );
// Go to the new page
Return ("Html/" + filename + ". html ");
}

Public string uploadDoc (System. Web. UI. HtmlControls. HtmlInputFile uploadFiles)
{
If (uploadFiles. PostedFile! = Null)
{
String fileName = uploadFiles. PostedFile. FileName;
String fileExtension = System. IO. Path. GetExtension (fileName). ToLower ();
String newfileName = "";

Try
{
// Verify whether it is in word format
If (fileExtension = ". doc" | fileExtension = "docx ")
{
DateTime now = DateTime. Now;
NewfileName = now. year. toString () + now. month. toString () + now. day. toString () + now. hour. toString () + now. minute. toString () + now. second. toString ();
// The upload path refers to the Doc path under the directory at the same level on the current upload page
UploadFiles. PostedFile. SaveAs (System. Web. HttpContext. Current. Server. MapPath ("Doc/" + newfileName + fileExtension ));
}
Else
{
Return "1 ";
}
}
Catch
{
Return "0 ";
}
Return System. Web. HttpContext. Current. Server. MapPath ("Doc/" + newfileName + fileExtension );
}

Else
{
Return "0 ";
}
}

Protected void btnUpload_Click (object sender, EventArgs e)
{
If (File1.PostedFile. ContentLength> 0)
{
Try
{
Doc2Html (File1 );
}
Catch (Exception exc)
{
Response. Write ("Error: <br/>" + exc. ToString () + ".");
}
Finally
{
Response. Write ("congratulations, switch successful! ");
Repeater1.DataBind ();
}
}
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.