zoukankan      html  css  js  c++  java
  • 一个简单的文件上传功能控件(原创)

     

    总在用别人的控件,第一次想自己写个控件。于是写了个简单的小控件,主要是用于自己学习和其他想尝试写控件的朋友,请多多指教。
    改控件主要作用是将本地图片上传到自定义目录。

      1using System;
      2using System.Web.UI;
      3using System.Web.UI.WebControls;
      4using System.ComponentModel;
      5using System.Web.UI.HtmlControls;
      6using System.IO;
      7
      8namespace UpLoadImage
      9{
     10 /// <summary>
     11 ///作者:DarkAngel 2004-10-27日创建
     12 ///支持图片上传到服务器功能
     13 /// </summary>

     14 [DefaultProperty("Text"),
     15  ToolboxData(@"<{0}:UpImage runat=server></{0}:UpImage>")]
     16 public class UpImage : Control, INamingContainer 
     17 {
     18  protected int filelength;
     19  protected string imageUrl;
     20  protected string mydirectory;
     21  static string LogoURL;
     22  protected string vpicture;
     23  public Button mybutton;
     24  public HtmlInputFile fileUpload;
     25  public Label Label1;
     26  public UpImage()
     27  {
     28   this.EnsureChildControls();
     29     
     30  }

     31  [Bindable(true),
     32  Category("Appearance"),
     33  DefaultValue("")]
     34  [
     35  DescriptionAttribute("文件大小")
     36  ]
     37  public int FileLength
     38  {
     39   set{filelength=value;}
     40   get{return filelength;}
     41  }

     42  
     43     [
     44  DescriptionAttribute("图片名字")
     45  ]
     46  public string ImageUrl
     47  {
     48   set{imageUrl=value;}
     49   get{return imageUrl;}
     50  }

     51  
     52  [
     53  DescriptionAttribute("文件路径")
     54  ]
     55  public string MyDirectory
     56  {
     57   get{return mydirectory;}
     58   set{mydirectory=value;}
     59  }

     60  
     61  [
     62  DescriptionAttribute("图片的相对地址")
     63  ]
     64  public string Logo
     65  {
     66   
     67   get{return LogoURL;}
     68   set{LogoURL=value;}
     69  }

     70  
     71  [
     72  DescriptionAttribute("是否显示图片")
     73  ]
     74  public string vPicture
     75  {
     76   set{vpicture=value;}
     77   get{return vpicture;}
     78  }

     79
     80  private void mybutton_Click(object sender, System.EventArgs e)
     81  {
     82   
     83   if(!fileUpload.Value.ToString().Equals(""))
     84   {
     85    LogoURL=fileUpload.PostedFile.FileName.ToString();
     86    
     87    LogoURL=LogoURL.Substring(LogoURL.LastIndexOf("."),(LogoURL.Length-LogoURL.LastIndexOf(".")));
     88    if(fileUpload.PostedFile.ContentLength>filelength)
     89    {
     90     myScript("图片超过指定大小!");
     91
     92    }

     93    else
     94    {
     95     if(LogoURL.Equals(".jpg"|| LogoURL.Equals(".bmp"|| LogoURL.Equals(".gif"))
     96     {
     97
     98      LogoURL=mydirectory+"\\"+imageUrl+LogoURL;
     99      mydirectory=Page.Server.MapPath(" ")+"\\"+mydirectory;
    100
    101      
    102      if(Directory.Exists(mydirectory))
    103      {
    104      }

    105      else
    106      {
    107       Directory.CreateDirectory(mydirectory);
    108      }

    109      fileUpload.PostedFile.SaveAs(Page.Server.MapPath(" ")+"\\"+LogoURL);
    110     
    111      
    112      
    113
    114      if(vpicture.Equals("1"))
    115      {
    116       Label1.Text="<img width='100' heigth='100' src='"+LogoURL+"'>";
    117      }

    118
    119      myScript("图片上传成功!");
    120 
    121     }

    122     else
    123     {
    124      myScript("文件类型不对!");
    125      
    126     }

    127
    128    }

    129   }

    130  
    131  }

    132  protected void myScript(string java)
    133  {
    134   Page.RegisterStartupScript("fsf","<script language=javascript>alert('"+java+"');</script>");
    135
    136  }

    137
    138  protected override void CreateChildControls() 
    139  {
    140   mybutton=new Button();
    141   fileUpload=new HtmlInputFile();
    142   Label1=new Label();
    143   mybutton.Text="提交";
    144    
    145   this.Controls.Add(fileUpload);
    146   this.Controls.Add(mybutton);
    147   this.Controls.Add(new LiteralControl("<p>"));
    148   this.Controls.Add(Label1);
    149   this.Controls.Add(new LiteralControl("</p>"));
    150   mybutton.Click+=new EventHandler(mybutton_Click);
    151   
    152  }

    153 }

    154}

    155
  • 相关阅读:
    反射学习笔记
    路由机制
    缓存笔记
    进程和线程
    垃圾回收机制
    堆和栈
    值类型和引用类型及参数传递
    招到一个程序员很难吗?
    全面讲解:委托、事件
    struts2入门实例
  • 原文地址:https://www.cnblogs.com/DarkAngel/p/175899.html
Copyright © 2011-2022 走看看