//  Upimage 当前路径下的文件夹名称  该文件夹用来存储上传的图片。

 

using System.IO; 命名空间引用

 

  private void Upimage()
        {
            if (FileUpload1.HasFile)
            {
               string fileContentType=FileUpload1.PostedFile.ContentType;  获取文件类型
              

 if (fileContentType=="image/jpeg"||fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg" || fileContentType == "image/swf")
               {    判断文件类型
                   if (FileUpload1.PostedFile.ContentLength / 1024 < 2000)
                   {    判断文件大小
                       string name = FileUpload1.PostedFile.FileName;
                       FileInfo file = new FileInfo(name);
                       string filename = System.DateTime.Now.ToString().Replace("-", "").Replace(" ", "").Replace(":", "") + file.Name;   获取当前时间值做前缀,避免命名一致。
                       string pathss = Server.MapPath("Upimage") + "\\" + filename;
                       try
                       {
                           FileUpload1.PostedFile.SaveAs(Server.MapPath("Upimage") + "\\" + filename);
                       }
                       catch(Exception ex)
                       {
                           ex.Message.ToString();
                       }
                  
                   }
               }
            }
        }


本文转载:CSDN博客