温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:多用户文件上传管理程序源码
当前文件:
FileSystem/edit.aspx.cs,打开代码结构图
FileSystem/edit.aspx.cs,打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Collections; 5
using System.Web; 6
using System.Web.Security; 7
using System.Web.UI; 8
using System.Web.UI.WebControls; 9
using System.Web.UI.WebControls.WebParts; 10
using System.Web.UI.HtmlControls; 11
using System.IO; 12
//该源码下载自www.51aspx.com(51aspx.com) 13
14
public partial class getfile : System.Web.UI.Page 15
{ 16
string text = "";//用来传递给原始数据变量'filetext'的临时变量,页面第一次加载时初始值.将来用做恢复用的 17
protected void Page_Load(object sender, EventArgs e) 18
{ 19
if (!User.Identity.IsAuthenticated) Response.Redirect("default.aspx");//如果用户没有登陆...... 20
if (Request.Url.AbsoluteUri.IndexOf("public") != -1)//表示能在地址栏找到表示为VIP用户的标识 21
{ 22
this.HyperLink1.Text = "支持外部匿名访问:"; 23
this.url.Visible = true; 24
this.url.Text = this.HyperLink1.NavigateUrl = Uri.UnescapeDataString(Request.Url.AbsoluteUri.Replace("edit.aspx?", ""));//由于这里传过来的地址比较巧妙,只要把'edit.aspx?'去掉,就是文件地址 25
this.HyperLink1.Target = "_blank"; 26
} 27
this.encode.Text = "目前编码:" + getencode().EncodingName; 28
if (!IsPostBack) 29
{ 30
//首先就是取文件地址,这不用说了吧... 31
ViewState["path"] = Uri.UnescapeDataString(Request.Url.Query.Replace("?", "")); 32
if (Request.Url.Query != "") readfile();//开始读取文件 33
ViewState["filetext"] = text;//读取过文件后变量'text'就有值了 34
this.filetext.Attributes.Add("style", "display:none"); 35
} 36
} 37
/* 读取文件 38
* 写入变量text,做恢复文件用 39
* 将按照'DropDownList1'控件指定的编码显示 40
* 将按照''控件指定的代码显示代码高亮格式 41
*/ 42
private void readfile() 43
{ 44
string noedit = ""; 45
string path = Server.MapPath(ViewState["path"].ToString()); 46
if (File.Exists(path)) 47
{ 48
string k1 = ".vb .asp .inc .sql .php .java .css .aspx .cs .txt .html .htm"; 49
string[] allkzm = k1.Split(new char[] { ' ', ' ' }); 50
string thiskzm = Path.GetExtension(path);//获取要修改的文件的扩展名 51
foreach (string kz in allkzm) 52
if (String.Equals(kz, thiskzm, StringComparison.OrdinalIgnoreCase))//与动态数组每个值全文匹配 53
{ 54
text = File.ReadAllText(path, getencode()); 55
noedit = ""; 56
break; 57
} 58
else noedit = text = "该文件不可编辑"; 59
} 60
else noedit = text = "文件不存在"; 61
if (noedit != "") 62
this.msg.Text = "<script type=\"text/javascript\" defer=\"defer\">alert('" + noedit + "');window.close()</script>"; 63
else 64
this.filetext.Text = text; 65
} 66
//选择语言后从新绑定 67
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 68
{ 69
readfile(); 70
} 71
//保存文件的操作 72
protected void Button1_Click(object sender, EventArgs e) 73
{ 74
string path = Server.MapPath(ViewState["path"].ToString()); 75
string text = this.filetext.Text; 76
StreamWriter sw = new StreamWriter(path, false, getencode()); 77
sw.Write(text); 78
sw.Close(); 79
} 80
protected void Button2_Click(object sender, EventArgs e) 81
{ 82
if (ViewState.Count > 0) this.filetext.Text = ViewState["filetext"].ToString(); 83
else this.msg.Text = "对不起,文件没有恢复成功!请检查您的使用方式"; 84
} 85
private System.Text.Encoding getencode() 86
{ 87
switch (this.DropDownList1.SelectedValue) 88
{ 89
case "0": 90
return System.Text.Encoding.GetEncoding("gb2312"); 91
case "1": 92
return System.Text.Encoding.UTF8; 93
default: 94
return System.Text.Encoding.Default; 95
} 96
} 97
} 98
//51aspx.com





}
}