標籤:上傳檔案 記事本提取目錄 c#刪除檔案
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;
}
}
}
本文出自 “技術人生” 部落格,請務必保留此出處http://zhaoyingyatou.blog.51cto.com/7151735/1433067