HTML:<Input id = "fi_fp" runat = "server" name = "fi_fp" type = "file"/>
. CS:
Using system. IO;
Using system. Text. regularexpressions;
Using system. Windows. forms;
If (fi_fp.postedfile.contentlength> 0)// Upload a new book
{
B. _ upfile =Uploadfile(Fi_fp );
If (istxt = true)
{B. _ txtlist =Readtxtmm(B. _ upfile);} // re-extract the Directory
}
Public StringUploadfilE(HtmlinputfileFile1)
{
Comm nadd = new comm ();
If (file1.postedfile. contentlength> 0)
{
String filenamec = NULL;
String fn = file1.postedfile. filename;
Filenamec = "file _" + nadd. getrandomfilename ();
Filenamec + = fn. substring (fn. lastindexof ("."));
String ext = fn. substring (fn. lastindexof (".") + 1). tolower ();
If (EXT = "TXT") {istxt = true ;}
If (Set(EXT ))
{
String picpath = system. configuration. configurationmanager. deleetpipeline ["bookfile"]. tostring (). Trim ();
File1.postedfile. saveas (httpcontext. Current. server. mappath (picpath) + filenamec );
Deleteoldfile(Server. mappath ("../bookfile/") + session ["upfile"]);// Delete the old file
Return filenamec;
}
Else
{
The return format is incorrect! ";
}
}
Else
{
Return "";
}
}
Public voidDeleteoldfile(String oldfile)
{
// Determine whether a file exists
If (file. exists (@ "" + oldfile + ""))
{
// Delete if any
File. Delete (@ "" + oldfile + "");
}
}
Public boolSet(String extion)
{
String [] extary =
{"TXT", "Doc", "ppt", "jpg", "pdf", "BMP", "docx"
};
For (INT I = 0; I <extary. length; I ++)
{
If (extary [I] = extion)
{
Return true;
}
}
Return false;
}
// Extract the chapter name and start row number and end row number
Public StringReadtxtmm(String URL)// Url txt file path
{
String Path = server. mappath ("../bookfile/") + URL;
String STR = "chapter *"; // search for the keyword * in the Section as a variable
String WZ = ""; // list of chapters and line numbers
String [] stringlines = file. readalllines (path, system. Text. encoding. Default );
RegEx Reg = new RegEx (STR );
For (INT I = 0; I <stringlines. length; I ++)
{
Match MAT = reg. Match (stringlines [I]. tostring ());
If (mat. Success)
{
If (I! = 0)
{
WZ + = "," + (I-1) + "|" + I + "," + stringlines [I]. tostring (); // the end line number of the previous chapter. the start line number chapter name of this chapter.
}
Else
{WZ + = "0," + stringlines [I]. tostring ();}
}
}
WZ + = "," + stringlines. Length + "| ";
Return WZ;
}
// No one reads the Directory and outputs it
// Read chapter content
String Path = server. mappath ("bookfile/") + URL;
String [] stringlines = file. readalllines (path, system. Text. encoding. Default );
Booktitle = stringlines [start]. tostring ();
For (INT I = start + 1; I <= end; I ++)
{
If (I <stringlines. length)
{
If (stringlines [I]. tostring (). Trim (). length> 0)
{
Bookscontent. Text + = "<p>" + stringlines [I]. tostring () + "</P> ";
}
}
}
// Read the next page of the previous page
String [] list = txtlist. Split ('| ');
For (INT I = 0; I <list. length; I ++)
{
If (list [I]. tostring (). Trim ()! = "")
{
String [] Dang = list [I]. Split (',');
If (Int. parse (Dang [0]. tostring () = start)
{
If (I-1> = 0)
{
String [] arrlist = list [I-1]. Split (',');
Uppage = "Previous Page: <a href = \" workerstudyview. aspx? Blid = "+ blid +" & START = "+ arrlist [0]. tostring () + "& End =" + arrlist [2]. tostring () + "\"> "+ arrlist [1]. tostring (). trim () + "</a> <br/> ";
}
If (I + 1 <list. Length-1)
{
String [] arrlist = list [I + 1]. Split (',');
Nextpage = "next page: <a href = \" workerstudyview. aspx? Blid = "+ blid +" & START = "+ arrlist [0]. tostring () + "& End =" + arrlist [2]. tostring () + "\"> "+ arrlist [1]. tostring (). trim () + "</a> <br/> ";
}
Break;
}
}
}
This article from the "technical life" blog, please be sure to keep this source http://zhaoyingyatou.blog.51cto.com/7151735/1433067