读取文件,上传文件,下载文件

//  读取文件信息
string FileCate = System.Configuration.ConfigurationManager.AppSettings["fileCatelog"];
        string FielPath = Server.MapPath("") +"\\"+ FileCate+"\\";       
        string[] fName = Directory.GetFiles(FielPath);
           for (int i = 0; i < fName.Length; i++)
        {        
            string fn="";
            long len = 0;
            string creatdate = "";         
            FileInfo fi1 = new FileInfo(fName[i].ToString());
            if (fi1.Exists)
            {
                creatdate = fi1.CreationTime.ToString("yyyy年MM月dd日");
                len = fi1.Length;
                fileType = fi1.Extension.ToString();
            }

            fn = fName[i].Substring(fName[i].LastIndexOf("\\") + 1, fName[i].Length - fName[i].LastIndexOf("\\") - 1);            
        
        }
    //保存上传的文件
    private void UploadSaveAs()
    {
        int FileCount = Request.Files.Count;
        if (FileCount > 0)
        {
            string[] AFileName;
            string UploadPath;

            UploadPath =  System.Configuration.ConfigurationManager.AppSettings["fileCatelog"]+"\\";
            char[] de ={ '\\' };
            HttpPostedFile hpf = Request.Files[0];
            AFileName = hpf.FileName.Split(de);
            string UploadFileName = AFileName[AFileName.Length - 1];

            if (UploadFileName != "" && hpf.ContentLength > 0)
            {
                if (hpf.ContentLength > 20000 * 1024)
                {
                    Response.Write("<script language='javascript'>alert('文件太大,无法上传!');</script>");
                    return;
                }
                string FileCate = System.Configuration.ConfigurationManager.AppSettings["fileCatelog"];
                string FielPath = Server.MapPath("") + "\\" + FileCate + "\\";
                string[] fName = Directory.GetFiles(FielPath);
                for (int i = 0; i < fName.Length; i++)
                {
                    FileInfo fi1 = new FileInfo(fName[i].ToString());
                    if (fi1.Name == UploadFileName)
                    {
                        Response.Write("<script language='javascript'>alert('服务器上已经存在该文件,请选择其他文件或更改文件名重新上传');</script>");

                        //Response.Write("<script>alert('服务器上已经存在同名文件,请选择其他文件或更改文件名重新上传')</script>");

                        return;

                    }
                }
                string strFilepath = this.Request.FilePath;
                string strPath = Path.GetDirectoryName(strFilepath);
                string DestFileName = UploadPath + UploadFileName;
                try
                {
                    hpf.SaveAs(Server.MapPath(DestFileName));
                    this.lbMsg.Text = "文件上传成功!";

                }
                catch (Exception ex)
                {
                    this.lbMsg.Text = ex.ToString();
                }
            }
            else if (hpf.ContentLength >=0)
            {
                Response.Write("<script language='javascript'>alert('该文件内容为空,请选择其他文件!');</script>");
                 return;
            }
        }
    }
//下载文件 
1。超连接绑定到文件名
 ((HyperLink)this.GridView1.Rows[i].Cells[3].FindControl("HyperLink1")).NavigateUrl = FileCate + "/" + this.GridView1.Rows[i].Cells[0].Text;
2。直接写在table中
tc.Text = "<a title=\"点击下载\" href=\"" + 文件目录 + "\\" + 文件名+ "\">" + 文件名 + "</a>";

转载于:https://www.cnblogs.com/wemmay/archive/2006/11/01/546961.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值