一个简单的文件上传功能控件(原创)

本文介绍了一个用于ASP.NET的自定义图片上传控件,该控件允许用户将本地图片上传到服务器上的自定义目录,并提供了文件大小限制、图片格式验证等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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

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

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

 31InBlock.gif  [Bindable(true),
 32InBlock.gif  Category("Appearance"),
 33InBlock.gif  DefaultValue("")]
 34InBlock.gif  [
 35InBlock.gif  DescriptionAttribute("文件大小")
 36InBlock.gif  ]
 37InBlock.gif  public int FileLength
 38ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
 39ExpandedSubBlockStart.gifContractedSubBlock.gif   setdot.gif{filelength=value;}
 40ExpandedSubBlockStart.gifContractedSubBlock.gif   getdot.gif{return filelength;}
 41ExpandedSubBlockEnd.gif  }

 42InBlock.gif  
 43InBlock.gif     [
 44InBlock.gif  DescriptionAttribute("图片名字")
 45InBlock.gif  ]
 46InBlock.gif  public string ImageUrl
 47ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
 48ExpandedSubBlockStart.gifContractedSubBlock.gif   setdot.gif{imageUrl=value;}
 49ExpandedSubBlockStart.gifContractedSubBlock.gif   getdot.gif{return imageUrl;}
 50ExpandedSubBlockEnd.gif  }

 51InBlock.gif  
 52InBlock.gif  [
 53InBlock.gif  DescriptionAttribute("文件路径")
 54InBlock.gif  ]
 55InBlock.gif  public string MyDirectory
 56ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
 57ExpandedSubBlockStart.gifContractedSubBlock.gif   getdot.gif{return mydirectory;}
 58ExpandedSubBlockStart.gifContractedSubBlock.gif   setdot.gif{mydirectory=value;}
 59ExpandedSubBlockEnd.gif  }

 60InBlock.gif  
 61InBlock.gif  [
 62InBlock.gif  DescriptionAttribute("图片的相对地址")
 63InBlock.gif  ]
 64InBlock.gif  public string Logo
 65ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
 66InBlock.gif   
 67ExpandedSubBlockStart.gifContractedSubBlock.gif   getdot.gif{return LogoURL;}
 68ExpandedSubBlockStart.gifContractedSubBlock.gif   setdot.gif{LogoURL=value;}
 69ExpandedSubBlockEnd.gif  }

 70InBlock.gif  
 71InBlock.gif  [
 72InBlock.gif  DescriptionAttribute("是否显示图片")
 73InBlock.gif  ]
 74InBlock.gif  public string vPicture
 75ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
 76ExpandedSubBlockStart.gifContractedSubBlock.gif   setdot.gif{vpicture=value;}
 77ExpandedSubBlockStart.gifContractedSubBlock.gif   getdot.gif{return vpicture;}
 78ExpandedSubBlockEnd.gif  }

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

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

105InBlock.gif      else
106ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
107InBlock.gif       Directory.CreateDirectory(mydirectory);
108ExpandedSubBlockEnd.gif      }

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

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

122InBlock.gif     else
123ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif{
124InBlock.gif      myScript("文件类型不对!");
125InBlock.gif      
126ExpandedSubBlockEnd.gif     }

127InBlock.gif
128ExpandedSubBlockEnd.gif    }

129ExpandedSubBlockEnd.gif   }

130InBlock.gif  
131ExpandedSubBlockEnd.gif  }

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

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

153ExpandedSubBlockEnd.gif }

154ExpandedBlockEnd.gif}

155 None.gif

转载于:https://www.cnblogs.com/DarkAngel/archive/2005/06/17/175899.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值