磁盘文件显示与上传

方法1: 
读取本地磁盘文件显示所有文件名代码如下:   
    //ClientDir下载目录
    public Hashtable PopulateDirectoryTree(string ServerDir, string ClientDir, string filtFile)
    {
        FileHashtable.Clear();
        DirectoryInfo di = new DirectoryInfo(Server.MapPath(ServerDir));
        Files = Directory.GetFiles(di.ToString());
        foreach (string fs in Files)
        {
            if (filtFile != "")
            {
                if (fs.Trim().Substring(fs.Trim().LastIndexOf(".") + 1).ToUpper() != filtFile)
                {
                    FileHashtable.Add(fs.Trim().Substring(fs.Trim().LastIndexOf("\\") + 1, fs.Trim().LastIndexOf(".") - fs.Trim().LastIndexOf("\\") - 1), fs.Trim().Substring(fs.Trim().LastIndexOf("\\") + 1));
                    flag = true;
                }
            }
            else
            {
                FileHashtable.Add(fs.Trim().Substring(fs.Trim().LastIndexOf("\\") + 1, fs.Trim().LastIndexOf(".") - fs.Trim().LastIndexOf("\\") - 1), fs.Trim().Substring(fs.Trim().LastIndexOf("\\") + 1));
                flag = true;
            }
        }
        GLZTUL = GetString(FileHashtable, ClientDir);
        return FileHashtable;
    }
    private string GetString(Hashtable ht,string ClientDir)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<ul>");
        foreach (DictionaryEntry de in ht)
        {
            sb.Append("<li>");
            sb.Append("<a href=\"" + ClientDir + "" + de.Value + "\" target=\"_blank\">" + de.Key + "</a>");
            sb.Append("</li>");
        }
        sb.Append("</ul>");
        return sb.ToString();
    }
使用方法实例如下:
        //PopulateDirectoryTree("0\\省公司", "0/省公司/", "");//广东电网公司线损管理制度
        //this.ltglzd.Text = GLZTUL;
        //PopulateDirectoryTree("0", "0/", "PDF");//广州供电局线损管理制度
        //this.ltgzxs.Text = GLZTUL;
        //PopulateDirectoryTree("0", "0/", "DOC");//广州供电局线损四分管理领导小组和工作小组
        //this.Literal3.Text = GLZTUL;
        //PopulateDirectoryTree("0\\分析制度", "0/分析制度/", "");//广州供电局线损分析管理制度
        //this.Literal4.Text = GLZTUL;
        //PopulateDirectoryTree("0\\考核办法", "0/考核办法/", "");//广州供电局线损考核办法
        //this.Literal5.Text = GLZTUL;
        //PopulateDirectoryTree("0\\实施细则", "0/实施细则/", "");//广州供电局线损四分实施细则
        //this.Literal6.Text = GLZTUL;


上传文件代码如下:
           protected void uploadFile_Click(object sender, EventArgs e)
    {
        string UpFilePath = string.Empty;
        if (fileType.SelectedValue == "0")
        {
            UpFilePath = Server.MapPath("0\\省公司");
        }
        else if (fileType.SelectedValue == "1")
        {
            UpFilePath = Server.MapPath("0");
        }
        else if (fileType.SelectedValue == "2")
        {
            UpFilePath = Server.MapPath("0");
        }
        else if (fileType.SelectedValue == "3")
        {
            UpFilePath = Server.MapPath("0\\分析制度");
        }
        else if (fileType.SelectedValue == "4")
        {
            UpFilePath = Server.MapPath("0\\考核办法");
        }
        else
        {
            UpFilePath = Server.MapPath("0\\实施细则");
        }
        this.UploadFile(UpFilePath);
        Bind();
    }
    private string UploadFile(string UpFilePath)
    {
        if (!Directory.Exists(UpFilePath))
        {
            DirectoryInfo dirinfo = Directory.CreateDirectory(UpFilePath);
        }
        string file_name = "";
        try
        {
            MyUploader.HttpMedia media = new MyUploader.HttpMedia();
            media.SetMaxLength(30000);
            ArrayList al = new ArrayList();
            al.Add("doc");
            al.Add("pdf");
            al.Add("xls");
            al.Add("mdb");
            al.Add("txt");
            al.Add("ppt");
            media.SetFilter(al);
            if (media.Upload(UpFilePath))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "e", "<script>alert('上传成功!')</script>");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "e", "<script>alert('上传失败,请检查文件格式!')</script>");
                string s = media.ErrorMessages;
            }
        }
        catch (Exception Err)
        {
            string Message = Err.Message;
        }
        return file_name;
    }

方法二:

 private void SaveAttFile()
        {
            int i;
            string vFileName = "";
            string vPath ;
            System.Web.HttpFileCollection vFiles=System.Web.HttpContext.Current.Request.Files;
            System.Web.HttpPostedFile vFile;
            vPath = Request.MapPath("attachFiles");
            if (!System.IO.Directory.Exists(vPath))
            {
                System.IO.Directory.CreateDirectory(vPath);
            }
            vPath = Request.MapPath("attachFiles/" + mYCID);
            if (!System.IO.Directory.Exists(vPath))
            {
                System.IO.Directory.CreateDirectory(vPath);
            }
            for (i = 0; i < vFiles.Count; i++)
            {
                vFile = vFiles[i];
                if (vFile.FileName == "")
                    continue;
                vFileName=System.IO.Path.GetFileName(vFile.FileName);
                vFile.SaveAs(vPath + "\\" + vFileName);
            }
            if (this.txtFiles.Text != "")
            {
                string[] vTxtFiles = this.txtFiles.Text.Split(new char[] { '|' });
                string[] vSQLS = new string[vTxtFiles.Length + 1];
                vPath = "attachFiles/" + mYCID + "/";
                vSQLS[0] = "delete from xskh_attachFiles where ycid=" + mYCID;

                for (i = 0; i < vTxtFiles.Length; i++)
                {
                    vSQLS[i + 1] = "insert into xskh_attachFiles (ycid,filename) values (" + mYCID + ",'" + vTxtFiles[i] + "')";
                    mAtFilesHtml += "&nbsp;<span><a target=\"_blank\" href=\"" + vPath + vTxtFiles[i] + "\">" + vTxtFiles[i] + "</a>&nbsp;<a href='javascript:void(0)' onclick=\"DelFile('" + vTxtFiles[i] + "',this)\">删除</a></span>";
                }
                DbConnect.Instance.ExecSQL(vSQLS);
            }
            else
            {
                DbConnect.Instance.ExecSQL("delete from xskh_attachFiles where ycid=" + mYCID, "main");
            }

        }


posted @ 2009-08-11 09:24  XGU_Winner  阅读(266)  评论(0编辑  收藏  举报