111,131
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebShop
{
/// 〈summary>
/// filesystem 的摘要说明。
/// 〈/summary>
public class filesystem : System.Web.UI.Page
{
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.DataList DataList1;
protected System.Web.UI.WebControls.Button Button3;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.HtmlControls.HtmlInputFile fileFeild1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Bind();
}
}
private void Bind()
{
//接收传过来的文件路径,为空则 初始化为目录下的“FileSystem”文件夹
//不为空,接受值,并用label1显示
string initpath="";
if(Request["path"]==null)
{
initpath=Server.MapPath("FileSystem");
}
else
{
initpath=Request["path"];
}
this.Label1.Text=initpath;
DataTable dt=new DataTable();
// 动态为DT添加String类型的两列,列名分别为Image和Name
DataColumn dc0=new DataColumn("Image",System.Type.GetType("System.String"));
dt.Columns.Add(dc0);
DataColumn dc1=new DataColumn("Name",System.Type.GetType("System.String"));
dt.Columns.Add(dc1);
DirectoryInfo di=new DirectoryInfo(this.Label1.Text);
//遍历文件夹
DirectoryInfo[] dis=di.GetDirectories();
foreach(DirectoryInfo d in dis)
{
//DT动态添加行,每一个文件夹一行
//第一列是像册图标和路径,第二列是像册名
DataRow dr=dt.NewRow();
dr[0]="〈a href='filesystem.aspx?path="+HttpUtility.UrlEncode(d.FullName,System.Text.Encoding.UTF8)+"'>〈img src='images/folder.gif' border=0/>〈/a>";
dr[1]=d.Name;
dt.Rows.Add(dr);
}
// 和上面一样,不过这次遍历的是文件
FileInfo[] fis=di.GetFiles();
foreach(FileInfo f in fis)
{
//获取文件的扩展名,转为小写
string ex=f.Extension.ToLower();
//如果文件是图片类型
if(ex==".jpg" || ex==".jpeg" || ex==".gif" || ex==".png" || ex==".bmp")
{
string fullname=f.FullName; //获取长路径
string urlpath = fullname.Substring(fullname.IndexOf("FileSystem")); //截取"FileSystem"之后的路径
string url=HttpUtility.UrlEncode(urlpath,System.Text.Encoding.UTF8); //将上面路径URL编码
DataRow dr=dt.NewRow();
dr[0]="〈a href='"+url+"' target='_blank'>〈img src='"+url+"' border=0 width=100 height=128/>〈/a>";
dr[1]=f.Name;
dt.Rows.Add(dr);
}
}
//DATALIST1绑定数据源
this.DataList1.DataSource=dt;
this.DataList1.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// 〈summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// 〈/summary>
private void InitializeComponent()
{
this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
this.Button3.Click += new System.EventHandler(this.Button3_Click);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void LinkButton1_Click(object sender, System.EventArgs e)
{ //检索this.Label1.Text显示目录的父目录
string Parent=Directory.GetParent(this.Label1.Text).ToString();
if(Parent.IndexOf("FileSystem")>-1)
{
//如果父目录包含FileSystem,URL定位到Parent路径
Response.Redirect("filesystem.aspx?path="+Parent);
}
else
{ //否则 退出
return;
}
}
//==============上传图象============================//
private void Button1_Click(object sender, System.EventArgs e)
{ //获取上传图片的路径
HttpPostedFile hpf=this.fileFeild1.PostedFile;
string ClientPath=hpf.FileName;
string filename=Path.GetFileName(ClientPath);
//获取文件扩展名
string ex=Path.GetExtension(filename);
//验证上传的文件是否为图片格式,图片则上传到服务器
if(ex==".jpg" || ex==".jpeg" || ex==".gif" || ex==".png" || ex==".bmp")
{
string SavePath=this.Label1.Text+"\\"+filename;
hpf.SaveAs(SavePath);
Bind();
}
else
{ //否则,提示
Response.Write(Tools.GetAlertJS("所上传的图片格式不正确!"));
return;
}
}
//===============创建新像册=============================//
private void Button2_Click(object sender, System.EventArgs e)
{
string filename=this.TextBox1.Text;
Directory.CreateDirectory(this.Label1.Text+"\\"+filename);
Bind();
}
//=================删除选中的像册或者图片==================//
private void Button3_Click(object sender, System.EventArgs e)
{
//遍历datalist1
for(int i=0;i<this.DataList1.Items.Count;i++)
{ //在datalist中查找名为Checkbox1的控件
//如果查找到的控件为选中状态,删除选中的像册或者图片,
if(((CheckBox)this.DataList1.Items[i].FindControl("CheckBox1")).Checked)
{
int index=this.DataList1.Items[i].ItemIndex;
string filePath=this.Label1.Text+"\\"+this.DataList1.DataKeys[index].ToString();
if(Directory.Exists(filePath))
{
Directory.Delete(filePath,true); //"true" 意思是删除文件夹里所有的图片和文件夹
}
if(File.Exists(filePath))
{
File.Delete(filePath);
}
Bind();
}
}
}
}
}