Through ASP. net Generation of static files there are a lot of online articles, and this site also has a lot of related articles and tutorials, usually ASP. net to generate static files, use the file stream to read the template content, replace the related keywords in the template content, and then generate static files. In this document, we use URLs to generate static files. Let's take a look at how it is implemented.
To create a testweb. aspx file, follow these steps:
Step 2: first reference the following command Space
Using system;
Using system. net;
Using system. IO;
Using system. text;
Step 2: Create a method to obtain the remote URL and generate a file. If the folder does not exist, the method is automatically created.
Code for obtaining a remote URL and generating a file:
C # code [http://www.xueit.com]
/// <Summary> /// generate a webpage file /// </Summary> /// <Param name = "url"> remote URL </param> /// <param name = "FILENAME"> Generate file name path </param> // <Param name = "pagecode"> Target URL page encoding </param> protected void downurltofile (string URL, string filename, string pagecode) {try {// encoding encode = encoding. getencoding (pagecode); // request URL httpwebrequest Req = (httpwebrequest) webrequest. create (URL); // set timeout (10 seconds) req. time out = 10000; this. notfolderiscreate (filename); // get response httpwebresponse rep = (httpwebresponse) req. getresponse (); // create streamreader and streamwriter file stream object streamreader sr = new streamreader (rep. getresponsestream (), encode); streamwriter Sw = new streamwriter (server. mappath (filename), false, encode); // write the content SW. write (Sr. readtoend (); // clears the current cache and writes the cache to the file SW. flush (); // release the resource SW of the object. close (); Sw. dispose (); Sr. close (); Sr. dispose (); response. write ("generated file" FILENAME "succeeded");} catch (exception ex) {response. write ("Generating File" FILENAME "failed, cause:" ex. message );}}
The key knowledge points of the above Code are to obtain remote URL data through httpwebrequest and httpwebresponse requests, and then use streamreader and streamwriter file streams to read and write data to the file. Note that there is also the encoding.
Code automatically created if the folder does not exist:
C # code [http://www.xueit.com]
/// <Summary> /// create if the folder does not exist /// </Summary> /// <Param name = "FILENAME"> file name path </param> protected void notfolderiscreate (string filename) {string fileatdir = server. mappath (path. getdirectoryname (filename); If (! Directory. exists (fileatdir) directory. createdirectory (fileatdir );}
Next we will look at how to call to generate a file.
Call the downurltofile () method in page_load to obtain the static file generated on the Baidu homepage.
C # code [http://www.xueit.com]
Protected void page_load (Object sender, eventargs e) {// call the method this. downurltofile ("http://www.baidu.com", "html/baidu.htm", "gb2312 ");}
Because the Baidu homepage is gb2312 encoded, enter gb2312 for the above call method. Let's take a look at the generated:
Run 1
Generated file
Open the generated static file
How are you doing.
With this method, you can easily generate static files through dynamic file URLs, such:
In the news and information table, article has an htmlfile field that stores the information file name, for example, HTML/news/20091224-001.html. After adding and saving the article in the background, call the following method:
Code [http://www.xueit.com]
DownUrltoFile("http://www.xueit.com/show.aspx?pid=1", "html/news/20091224-001.html", "GB2312");
Here URL: http://www.xueit.com/show.aspx? PID = 1 is the dynamic display of the article, HTML/news/20091224-001.html is the pre-saved file name of the table field htmlfile, so that you can generate a static file.
The above method is not to use a template to generate static files. I just want to change my mind. I suggest you come to my site and talk about it.