zoukankan      html  css  js  c++  java
  • FileUpLoad单击按钮,完成上传文件到服务器

    //该方法是按钮单击事件,完成上传文件到服务器
    private void btnUpload_Click(object sender, System.EventArgs e)
      {
       
    if(fileUpload1.PostedFile!=null && fileUpload1.PostedFile.FileName!="" &&    fileUpload1.PostedFile.ContentLength!=0)
        {
         
    if(fileUpload1.PostedFile.ContentLength > 5 * 1024 )
          {
        lblError.Text
    = "上传文件过大";
        lblError.Visible
    = true;
       
    return;
          }
         
    string sFileName = Path.GetExtension(fileUpload1.PostedFile.FileName).ToUpper();
         
    if(! (strFileName == ".BMP" || strFileName == ".GIF" || strFileName == ".JPG") )
          {
        lblError.Text
    = "文件格式不正确";
        lblError.Visible
    = true;
       
    return;
          }

          Random ran
    = new Random();
         
    string sNewImg = DateTime.Now.ToString(@"yyyyMMddHHmmss") + ran.Next(100,999) + Path.GetExtension(fileUpload1.PostedFile.FileName) ;
         
    string sPath = Server.MapPath( "~/Picture/" + sNewImg);
         
    if( !Directory.Exists(Path.GetDirectoryName(sPath) ) )
          {
            Directory.CreateDirectory(Path.GetDirectoryName(strPath));
          }
          fileUpload1.PostedFile.SaveAs(sPath);
        }       
      }

  • 相关阅读:
    74.Interesting Sequence(有趣的数列)(拓扑排序)
    CODEVS 1746 贪吃的九头龙
    NYOJ 110 剑客决斗
    CODEVS 2451 互不侵犯
    洛谷 P1896 互不侵犯King
    洛谷 P1066 2^k进制数
    洛谷 P1656 炸铁路
    洛谷 P1830 轰炸Ⅲ
    CODEVS 1051 接龙游戏
    POJ 3461 Oulipo
  • 原文地址:https://www.cnblogs.com/juan/p/1428829.html
Copyright © 2011-2022 走看看