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)//上传了新图书
{
b._upfile = uploadFile(Fi_Fp);
if (isTxt == true)
{ b._txtlist = readtxtmm(b._upfile); }//重新提取目录
}
public string uploadFile(HtmlInputFile File1)
{
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.AppSettings["bookfile"].ToString().Trim();
File1.PostedFile.SaveAs(HttpContext.Current.Server.MapPath(Picpath) + filenamec);
deleteoldfile(Server.MapPath("../bookfile/") + Session["upfile"]);//删除旧文件
return filenamec;
}
else
{
return "格式不正确!";
}
}
else
{
return "";
}
}
public void deleteoldfile(string oldfile)
{
//判断文件是不是存在
if (File.Exists(@""+oldfile+""))
{
//如果存在则删除
File.Delete(@"" + oldfile + "");
}
}
public bool SET(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;
}
//提取章节名及开始行号结束行号
public string readtxtmm(string url)//url txt文件路径
{
string path = Server.MapPath("../bookfile/") + url;
string str = "第*章";//查找章节关键字 *号为变量
string wz = ""; //章节及行号列表
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();//上一章结尾行号,本章开始行号 章节名
}
else
{ wz += "0," + stringlines[i].ToString(); }
}
}
wz += "," + stringlines.Length + "|";
return wz;
}
//读取目录没人并输出
//读取章节内容
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>";
}
}
}
//读取上一页下一页
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 = "上一页:<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 = "下一页:<a href=\"WorkerStudyView.aspx?BLid=" + blid + "&start=" + arrlist[0].ToString() + "&end=" + arrlist[2].ToString() + "\">" + arrlist[1].ToString().Trim() + "</a><br />";
}
break;
}
}
}
c# 上传文件删除旧文件 记事本提取目录