I have learned to extract file values from FSO and also learned to input information into the file. Then let’s apply it.
I don’t know if you have this habit: when you see a file, you unconsciously right-click and choose to open it with Notepad. Haha, almost no file is not possible. So now, it can be defaulted that all files are text, but the suffix names are different; that is, the content information of any file can be extracted now. OK, just imagine:
1. Extract the path of a file (use the file button to find and locate)
2. Open the path file and read all lines
3. Display the read information
1. viewcode.asp
| <% Function ShowCode(filename) Set fso = Server.CreateObject("Scripting.FileSystemObject") Set cnrs = fso.OpenTextFile(filename, 1) While Not cnrs.AtEndOfStream rsline = cnrs.ReadLine rsline = server.HTMLEncode(rsline) Response.Write(rsline & "<br>") Wend end Function %> <form action="viewcode.asp" method="post"> Enter the file name <input type="file" name="filename"> <input type="submit" value="View source program"> </form> <% file=request.form("filename") response.write (file & "source program as follows <hr>") If trim(file)<> "" then Call ShowCode(file) End If %> |
When debugging the above program, you can select the html and asp pages, or open any application, etc.
The defined ShowCode function is mainly used to open, read and display all information content in the file. Note that server.HTMLEncode(rsline) is added for files containing standard HTML code.
All lines in the display file are displayed in a conditional loop.
While Not cnrs.AtEndOfStream
...
Wend
Next, the following example specifically involves the open method. Remember? Under normal circumstances, open a file using fso.OpenTextFile("c:/testfile.txt",1), and the function of parameter 1 is: open the file in read-only mode. This file cannot be written. What should I do if a file already exists and needs to be added to it? Simple, the parameter is 8.
PS: There is also a way to read here.
<% whichfile=server.mappath("test.txt") Set fso = CreateObject("Scripting.FileSystemObject") Set txt = fso.OpenTextFile(whichfile,1) rline = txt.ReadAll rline=replace(Server.HtmlEncode(rline),Chr(13),"<br>") Response.Write rline txt.Close %> |
What's the use of this? Haha, this is how Amazon’s online storyline: if you can do it, you need to first display the original story, and then add the story to write it to the file yourself. The most important thing about writing files is to add writes. So it can be achieved below.
2. story.asp
<% If not request.Form("NextLine")="" then Set fso=Server.CreateObject("Scripting.FileSystemobject") textfile1=Server.MapPath("story.txt") set cnrs=fso.OpenTextFile(textfile1,8) cnrs.WriteLine(Request.Form("NextLine")) cnrs.Close end if %> The story is as follows: <% Set fso=Server.CreateObject("Scripting.FileSystemObject") textfile1=Server.MapPath("story.txt") set cnrs=fso.OpenTextFile(textfile1,1) While not cnrs.AtEndOfStream Response.Write "" & cnrs.ReadLine wend cnrs.close %> <hr> <form method="post" action="story.asp"> Please enter a new line for this story: <input name="NextLine" type="text" size="70"> <input type="submit" value="submit"> </form>
|