| 网站首页 | 文章中心 | 电子书下载 | 矢量图库 | 在线视频教程 | 素材下载 | 程序代码下载 | 视频教程下载 | JS代码 | 论坛 | 
龙腾教程网  
常用软件类:
|杀毒安全 |联络聊天 |网络软件 |多媒体类 |系统工具 |图形图像 |系统工具 |应用软件 |行业软件
开发设计类:
|动画制作 |图像处理 |3D设计 |操作系统 |站长学院 |网络相关 |WEB设计 |数据库类 |程序开发
 
  您当前位置:您现在的位置: 龙腾软件教程网 >> 文章中心 >> WEB设计 >> asp.net >> 编程实列 >> 文章正文 
 
asp.net 制作浏览服务器文件程序
作者:未知 文章来源:网络

本站原创 转载请注明 http://www.longtengwang.com 龙腾教程网

前台文件file.aspx

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="file.aspx.cs" Inherits="file" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table width="100%" border="0" cellspacing="0">
  <tr>
    <td height="60" colspan="2" align="center" bgcolor="#CCCCCC">文件管理器</td>
  </tr>
  <tr>
    <td height="25">当前路径:
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></td>
    <td width="200">
        &nbsp;<span style="color: crimson"><strong>返回上一层</strong></span><asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/ZA012330292052.gif"
            OnClick="ImageButton1_Click" /></td>
  </tr>
  <tr>
    <td colspan="2" style="height: 65px">&nbsp;<asp:Table ID="Table1" runat="server">
        </asp:Table>
    </td>
  </tr>
</table>
    </div>
    </form>
</body>
</html>

 

CS文件

 

 public partial class file : System.Web.UI.Page
{
    public string fpath;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)

        {
            if (Request.QueryString["fpath"] == null)
            {

                fpath = Server.MapPath(".");
                this.Label2.Text = fpath;
                BindFile(fpath);
            }
            else
            {
                fpath = Server.UrlDecode(Request.QueryString["fpath"].ToString());
                    this.Label2.Text=fpath;

                    BindFile(fpath);
            }
       
        }
    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="fpath"></param>
    protected void BindFile(string path)
    {
        TableCell td;
        TableRow tr;


        tr = new TableRow();

        td = new TableCell();
        td.Text = "文件名";
        td.Width=300;
        td.BackColor = Color.Aqua;
        tr.Cells.Add(td);

        td = new TableCell();
        td.Text = "大小";
        td.Width = 100;
        td.BackColor = Color.Aqua;
        tr.Cells.Add(td);

        td = new TableCell();
        td.Text = "类型";
        td.Width = 100;
        td.BackColor = Color.Aqua;
        tr.Cells.Add(td);

        td = new TableCell();
        td.Text = "发布时间";
        td.Width = 200;
        td.BackColor = Color.Aqua;
        tr.Cells.Add(td);

        td = new TableCell();
        td.Text = "操作";
        td.Width = 100;
        td.BackColor = Color.Aqua;
        tr.Cells.Add(td);

        Table1.Rows.Add(tr);

        string fname;
        string ftype;
        string ftime;
        string fsize;
        HyperLink hlink;
        string url;
        DirectoryInfo di = new DirectoryInfo(path);
        foreach(FileSystemInfo fsi in di.GetFileSystemInfos())
        {
            if (fsi is FileInfo)//如果是文件
            {
                FileInfo fi = (FileInfo)fsi;
                fname = fi.Name;
                if (fi.Extension.Length < 1)//如果扩展名小于一
                {
                    ftype = "";
                }
                else
                {
                    ftype = fi.Extension.Remove(0, 1);

                }


                fsize = fi.Length.ToString();
                ftime = fi.LastWriteTime.ToString();
            }
            else  //如果是文件夹
            {
                di = (DirectoryInfo)fsi;
                fname = di.Name;
                ftype = "文件夹";
                fsize = "";
                ftime = di.LastWriteTime.ToString();
           
            }
            tr = new TableRow();
            td = new TableCell();

            td.Controls.Add(new LiteralControl(fname));
            tr.Cells.Add(td);

            td = new TableCell();

            td.Controls.Add(new LiteralControl(ftype));
            tr.Cells.Add(td);

            td = new TableCell();

            td.Controls.Add(new LiteralControl(fsize));
            tr.Cells.Add(td);

            td = new TableCell();

            td.Controls.Add(new LiteralControl(ftime));
            tr.Cells.Add(td);

            if (fsi is DirectoryInfo)
            {
                hlink = new HyperLink();
                hlink.Text = "打开";
                url = Server.UrlEncode(fpath);
                hlink.NavigateUrl = "file.aspx?fpath=" + url + "\\" + fname;
                td = new TableCell();
                td.Controls.Add(hlink);
                tr.Cells.Add(td);

            }
            else
            {
                hlink = new HyperLink();
                hlink.Text = "编辑";
                url = Server.UrlEncode(fpath);
                hlink.NavigateUrl = "edit.aspx?fpath=" + url + "\\" + fname;
                td = new TableCell();
                td.Controls.Add(hlink);
                tr.Cells.Add(td);
           
            }

            Table1.Rows.Add(tr);
      }

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {

        if (fpath != Label2.Text.ToString())
        {

            string rootdir = Directory.GetParent(Label2.Text).FullName;
            fpath = rootdir;
            this.Label2.Text = fpath;
            BindFile(fpath);
        }
    }
}

 

注意需要引入对应的命名空间: 关于对文件处理和编辑这里不就叙述了.基本原理是利用IO打开传过来文件的路径的文件 然后编辑后保存就可以了

一个简单的文件浏览器就做好了 可以浏览服务器上的文件

最后效果:



上一篇:
  • 上一篇文章:
  • 下一篇
  • 下一篇文章: 没有了
  • 收藏此文到百度搜藏 百度搜藏| 新浪VIvi| 365key| Younote| 博采中心| 你好BLOG| 亿友网摘| 和讯网摘|
    相关文章    
    制作Asp.net+SQL server数据库集合安装程序
    asp.net 制作网液中文验证码技术详解代码
    ASP.Net利用International Pack实现中文简繁
    图解ASP.NET防盗链原理实现
    asp.net2.0实现跨站点共享Session解决方案
    .Net开发命名空间的命名规则
    ASP.NET2.0+SQL Server2005多层架构的应用
    Asp.net多层架构中的变量引用与传递
    ASP.NET 2.0轻松实现数据库应用开发
    ASP.NET实现DropDownList控件数据绑定第一项
     
     
     
    最新文章
    普通文章 asp.net 制作浏览服务器文件程序最新文章
    普通文章 JAVASCRIPT 实现通过弹出式对话框最新文章
    普通文章 photoshop绘制性感诱惑的红唇最新文章
    普通文章 正确选择多WAN路由器六大关键问题最新文章
    普通文章 常见ADSL;路由器登陆地址密码列表最新文章
    普通文章 Photoshop合成精彩的电影海报最新文章
    普通文章 javascript的几款不错的编辑器介最新文章
    普通文章 Dreamweaver CS4新特性之JavaScr最新文章
    普通文章 解决Firefox无法保存密码的问题最新文章
    普通文章 Windows Vista系统磁盘镜像工具使最新文章
     
    热门文章
    推荐文章 中秋佳节-photoshop手绘嫦娥奔月
     
     
    设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 

    版权所有2006-2008 龙腾教程网