导航

c#:FileUpload上传文件并创建文件夹

Posted on 2010-07-12 15:32  ykhi  阅读(12217)  评论(0)    收藏  举报

protected void Button10_Click(object sender, EventArgs e)
    {
        string name = FileUpload1.FileName;//获取文件名
        string name1 = FileUpload1.PostedFile.FileName; //获取完整客户端文件路径
        string type = FileUpload1.PostedFile.ContentType;//上传文件类型
        string size = FileUpload1.PostedFile.ContentLength.ToString();//上传文件大小
        string type2 = name.Substring(name.LastIndexOf(".") + 1);//上传文件后缀名
        string ipath = Server.MapPath("upload") + "\\" + name; //上传到服务器上后的路径(实际路径),"\\"必须为两个斜杠,在C#中一个斜杠表示转义符.
        string ipath1 = Server.MapPath("upload");//创建文件夹时用
        string wpath = "upload\\" + name; //虚拟路径
        if (type2 == "jpg" || type2 == "gif" || type2 == "bmp" || type2 == "png")//根据后缀名来限制上传类型
        {
            
            if (!System.IO.Directory.Exists(ipath1))//判断文件夹是否已经存在
            {
                System.IO.Directory.CreateDirectory(ipath1);//创建文件夹
            } 
            FileUpload1.SaveAs(ipath);//上传文件到ipath这个路径里
            
        }
    }