c# 上传关键代码

   //从控件上传文件   
        public void fileUpload()
        {
            System.Web.HttpFileCollection files = Request.Files;
            for (int fileCount = 0; fileCount < files.Count; fileCount++)
            {
                System.Web.HttpPostedFile postedfile = files[fileCount];

                string fileName = System.IO.Path.GetFileName(postedfile.FileName);
                if (!String.IsNullOrEmpty(fileName))
                {

                    string fileExtension = System.IO.Path.GetExtension(fileName);    //获取文件类型  

                    //上传目录  
                    string directory = Server.MapPath("/UpLoadFiles/");
                    //文件全路径  
                    string path = directory + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExtension;

                    //判断目录是否存在  
                    if (!Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(directory);
                    }
                    //文件存在就删除文件  
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                    //上传到服务器的路径  
                    postedfile.SaveAs(path);
            
                    File.Delete(path);//删除文件  
                }
            }
        }
原文地址:https://www.cnblogs.com/lampon/p/3652260.html