我的下载管理

作了几个小项目,经常有"下载天地"这个栏目。因为文件一般是从远程通过ftp传到服务器上的,在后台的管理上,当需要添加一个下载条目时,以前的办法是,需要手工添加下载路径和文件大小,特别麻烦。后来看到ftb1.6.5上传图片的例子后,觉得这种方法完全可以用到后台选择文件的场合。于是,用了一天的功夫,实现了这个功能。(小弟愚笨,花了1天时间,大家不要笑啊

filelist.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;

namespace hwtkd.manage
{
    
/// <summary>
    
/// filelist 的摘要说明。
    
/// </summary>

    public class filelist : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Panel Filepanel;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
if(!Page.IsPostBack)
            
{
                rootpath 
= Request.QueryString["root"];
                currentfolder 
= "";                    
            }

            
else
            
{
                rootpath 
= Request.Form["RootFolder"].ToString().Replace("\\""\");
                currentfolder 
= Request.Form["CurrentFolder"].ToString().Replace("\\""\");
            }


            RootFolder.Value 
= rootpath;
            CurrentFolder.Value 
= currentfolder;
            path 
= System.Web.HttpContext.Current.Request.PhysicalApplicationPath + rootpath + currentfolder;

            DisplayList();
        }


        
protected System.Web.UI.HtmlControls.HtmlInputHidden SelectedFile;
        
protected System.Web.UI.HtmlControls.HtmlInputHidden RootFolder;
        
protected System.Web.UI.HtmlControls.HtmlInputHidden CurrentFolder;

        
string rootpath;
        
string currentfolder;
        
string path;
        
protected System.Web.UI.HtmlControls.HtmlInputButton sub;

        
private static string[] extentionlist = ("ai|pdf|asp|txt|asx|avi|bmp|c|cda|cgi|cpp|cs|css|dsw|fla|gif|h|htm|html|java|jpg|js|midi|mov|mp3|mpg|php|png|psd|" + 
                
"psp|rar|rm|sln|swf|tiff|unkown|wav|wm|wma|wmd|wmv|xml|zip").Split('|');

        
private void DisplayList()
        
{            
            Filepanel.Controls.Clear();

            FileInfoList infolist 
= new FileInfoList(path);
            FileInfo[] filelist 
= infolist.FileList;
            DirectoryInfo[] directorylist 
= infolist.DirectoryList;

            
if(currentfolder != "")
            
{
                Panel holder 
= new Panel();    
                holder.CssClass 
= "imageholder";
                HtmlImage image 
= new HtmlImage();
                image.Src 
= "fileimages/parent.jpg";
                image.Attributes[
"class"= "img";
                image.Attributes[
"unselectable"]="on";
                image.Attributes[
"align"= "absmiddle";

                holder.Attributes[
"unselectable"]="on"
                holder.Attributes[
"onclick"]="divClick(this,'');";                 
                
if(currentfolder.IndexOf("\"== -1)
                
{
                    holder.Attributes[
"ondblclick"]="gotoFolder('" + rootpath + "','');"
                }

                
else
                
{                    
                    holder.Attributes[
"ondblclick"]=("gotoFolder('" + rootpath + "','" + currentfolder.Substring(0, currentfolder.LastIndexOf("\", currentfolder.Length - 2, currentfolder.Length - 1+ 1+ "');").Replace("\""\\");
                }

                holder.Controls.Add(image);

                Panel mainpanel 
= new Panel();                
                mainpanel.CssClass 
= "imagespacer";
                mainpanel.Controls.Add(holder);

                Panel foldertitle 
= new Panel();
                foldertitle.CssClass 
= "titleHolder";
                foldertitle.Controls.Add(
new LiteralControl("返回"));

                mainpanel.Controls.Add(foldertitle);

                Filepanel.Controls.Add(mainpanel);
            }


            
foreach(DirectoryInfo info in directorylist)
            
{
                Panel holder 
= new Panel();    
                holder.CssClass 
= "imageholder";
                HtmlImage image 
= new HtmlImage();
                image.Src 
= "fileimages/folder.jpg";
                image.Attributes[
"class"= "img";
                image.Attributes[
"unselectable"]="on"
                image.Attributes[
"align"= "absmiddle";
                
                holder.Attributes[
"unselectable"]="on"
                holder.Attributes[
"onclick"]="divClick(this,'');";                
                holder.Attributes[
"ondblclick"= ("gotoFolder('" + rootpath + "','" + currentfolder + info.Name + "\');").Replace("\""\\");
            
                holder.Controls.Add(image);

                Panel mainpanel 
= new Panel();                
                mainpanel.CssClass 
= "imagespacer";
                mainpanel.Controls.Add(holder);

                Panel foldertitle 
= new Panel();
                foldertitle.CssClass 
= "titleHolder";
                foldertitle.Controls.Add(
new LiteralControl(info.Name));

                mainpanel.Controls.Add(foldertitle);

                Filepanel.Controls.Add(mainpanel);
            }


            
foreach(FileInfo info in filelist)
            
{
                Panel holder 
= new Panel();    
                holder.CssClass 
= "imageholder";                 
                HtmlImage image 
= new HtmlImage();
                
string extention = info.Extension.ToLower().Substring(1);                
                
foreach(string str in extentionlist)
                
{
                    
if(str == extention)
                    
{
                        image.Src 
= "fileimages/" + extention + ".jpg";
                        
break;
                    }

                }

                
if(image.Src == "")
                
{
                    image.Src 
= "fileimages/unkown.jpg";
                }
                
                image.Attributes[
"class"= "img";
                image.Attributes[
"unselectable"]="on";
                image.Attributes[
"align"= "absmiddle";

                holder.Attributes[
"onclick"]="divClick(this,'" + info.Name + "');";
                holder.Attributes[
"ondblclick"]=("returnFile('" + rootpath + "','" + currentfolder + info.Name + "','" +FormatFileSize(info.Length) + "');").Replace("\""\\");
                holder.Attributes[
"unselectable"]="on";
                holder.Controls.Add(image);

                Panel mainpanel 
= new Panel();                
                mainpanel.CssClass 
= "imagespacer";
                mainpanel.Controls.Add(holder);

                Panel foldertitle 
= new Panel();
                foldertitle.CssClass 
= "titleHolder";
                foldertitle.Controls.Add(
new LiteralControl(info.Name + "<br>" + FormatFileSize(info.Length)));

                mainpanel.Controls.Add(foldertitle);

                Filepanel.Controls.Add(mainpanel);
            }

        }


        
private string GetRelativePath(string path, string path2)
        
{
            
return path.Replace(path2 + "\""");
        }


        
private string FormatFileSize(long length)
        
{
            
if(length < 0.5 * 1024 * 1024)
            
{                
                
return (length / 1024f).ToString("##0.00K");
            }

            
else
            
{
                
return ((float)length / (1024 * 1024)).ToString("##0.00M");
            }

        }


        
Web 窗体设计器生成的代码        

        
private void sub_ServerClick(object sender, System.EventArgs e)
        
{
            ;
        }


    }


    
public class FileInfoList
    
{
        
public FileInfoList()
        
{
            isruned 
= false;            
        }


        
public FileInfoList(string path):this()
        
{
            
this.path = path;
        }


        
private FileInfo[] filelist;
        
private DirectoryInfo[] directorylist;
        
private string path;
        
private bool isruned;

        
public string Path
        
{
            
get{return path;}
            
set{path = value;}
        }


        
public FileInfo[] FileList
        
{
            
get
            
{
                
if(!isruned)
                
{
                    RefreshInfo();
                }


                
return filelist;
            }

        }


        
public DirectoryInfo[] DirectoryList
        
{
            
get
            
{
                
if(!isruned)
                
{
                    RefreshInfo();
                }


                
return directorylist;
            }

        }
    

        
public void  RefreshInfo()
        
{    
            DirectoryInfo directory 
= new DirectoryInfo(path);

            filelist 
= directory.GetFiles();
            directorylist 
= directory.GetDirectories();

            isruned 
= true;
        }

        
    }

}



filelist.aspx
<%@ Page language="c#" Codebehind="filelist.aspx.cs" AutoEventWireup="false" Inherits="hwtkd.manage.filelist" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>filelist</title>
        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        
<meta name="CODE_LANGUAGE" Content="C#">
        
<meta name="vs_defaultClientScript" content="JavaScript">
        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
        
<style> BODY { BORDER-RIGHT: 0px; PADDING-RIGHT: 0px; BORDER-TOP: 0px; PADDING-LEFT: 0px; BACKGROUND: #ffffff; PADDING-BOTTOM: 0px; MARGIN: 0px; OVERFLOW: hidden; BORDER-LEFT: 0px; WIDTH: 100%; PADDING-TOP: 0px; BORDER-BOTTOM: 0px }
    BODY 
{ FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: "宋体",Verdana, Arial, Helvetica, sans-serif }
    TR 
{ FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: "宋体",Verdana, Arial, Helvetica, sans-serif }
    TD 
{ FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: "宋体",Verdana, Arial, Helvetica, sans-serif }
    DIV.imagespacer 
{ FLOAT: left; MARGIN: 5px; FONT: 10pt verdana; OVERFLOW: hidden; WIDTH: 120px; HEIGHT: 126px; TEXT-ALIGN: center }
    DIV.imageholder 
{ BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 0px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 100px; PADDING-TOP: 0px; BORDER-BOTTOM: #cccccc 1px solid; HEIGHT: 100px ;valign:absmiddle;}
    DIV.titleholder 
{ FONT-SIZE: 8pt; OVERFLOW: hidden; WIDTH: 100px; FONT-FAMILY: ms sans serif, arial; WHITE-SPACE: nowrap; TEXT-OVERFLOW: ellipsis }
    .img 
{ WIDTH: 72px; HEIGHT: 72px;}
    DIV.main
{
        width=100%; height
:100%; overflow: auto;
    
}

        
</style>
        
<script language="javascript">
lastDiv 
= null;
function divClick(theDiv,filename) {
    
if (lastDiv) {
        lastDiv.style.border 
= "1 solid #CCCCCC";
    }

    lastDiv 
= theDiv;
    theDiv.style.border 
= "2 solid #316AC5";
    
    document.getElementById(
"SelectedFile").value = filename;

}

function gotoFolder(rootfolder,newfolder) 
{
    document.getElementById(
"RootFolder").value = rootfolder;
    document.getElementById(
"CurrentFolder").value = newfolder;    
    __doPostBack('sub','');
}
        
function returnFile(rootfolder,filepath,filesize) {
    
var arr = new Array();
    arr[
"rootfolder"= rootfolder;  
    arr[
"filepath"= filepath;
    arr[
"filesize"= filesize;
    window.parent.returnValue 
= arr;
    window.parent.close();
}
        
        
</script>
        
<base target="_self">
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
            
<table width="100%" height="100%" border="0" cellpadding="3" cellspacing="0">
                
<tr>
                    
<td><asp:Panel id="Filepanel" runat="server" CssClass="main"></asp:Panel></td>
                
</tr>
                
<tr>
                    
<td height="16" style="padding-left:10px;border-top: 1 solid #999999; background-color:#99ccff;"><INPUT type="button" value="刷新" id="sub" runat="server"></td>
                
</tr>                
            
</table>
            
<input type="hidden" id="SelectedFile" runat="server"> <input type="hidden" id="RootFolder" Value="images" runat="server">
            
<input type="hidden" id="CurrentFolder" Value="images" runat="server">
        
</form>
    
</body>
</HTML>


filelist.aspx页面需要另外一个页面用调用
<script language=javascript>
        
function FindFile()
        {
            
var arr=window.showModalDialog("filelist.aspx?root=" + window.encodeURIComponent("upload\file\"), """dialogWidth:570px;status:no");
            
if(arr == null)
                
return;
            
var arr1=new String(arr["rootfolder"]);
            
var arr2=new String(arr["filepath"]);
            
var arr3 = new String(arr["filesize"]);
            document.getElementById(
"URL").value = format(arr1 + arr2);
            document.getElementById(
"Size").value = arr3;
        }
        
function format(strin)
        {
            strout
="";
            
for(i=0;i<strin.length;i++)
            {
                s
=strin.charAt(i);
                
if(s=='\')
                    strout
+="/";
                
else
                    strout
+=s;
            }
            
return strout;
        }
        
</script>

posted on 2004-08-16 21:52  sfsdfw  阅读(3143)  评论(4编辑  收藏  举报

导航