新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > .Net开发 > Asp.net教程 > 正文:一个简单的文件上传功能控件

一个简单的文件上传功能控件

新客网 XKER.COM 2004-11-16 来源: 收藏本文
总在用别人的控件,第一次想自己写个控件。于是写了个简单的小控件,主要是用于自己学习和其他想尝试写控件的朋友,请多多指教。
改控件主要作用是将本地图片上传到自定义目录。
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.HtmlControls;
using System.IO;

namespace UpLoadImage
{
/// <summary>
///作者:DarkAngel 2004-10-27日创建
///支持图片上传到服务器功能
/// </summary>
[DefaultProperty("Text"),
ToolboxData(@"<{0}:UpImage runat=server></{0}:UpImage>")]
public class UpImage : Control, INamingContainer
{
protected int filelength;
protected string imageUrl;
protected string mydirectory;
static string LogoURL;
protected string vpicture;
public Button mybutton;
public HtmlInputFile fileUpload;
public Label Label1;
public UpImage()
{
this.EnsureChildControls();

}
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
[
DescriptionAttribute("文件大小")
]
public int FileLength
{
set{filelength=value;}
get{return filelength;}
}

[
DescriptionAttribute("图片名字")
]
public string ImageUrl
{
set{imageUrl=value;}
get{return imageUrl;}
}

[
DescriptionAttribute("文件路径")
]
public string MyDirectory
{
get{return mydirectory;}
set{mydirectory=value;}
}

[
DescriptionAttribute("图片的相对地址")
]
public string Logo
{

get{return LogoURL;}
set{LogoURL=value;}
}

[
DescriptionAttribute("是否显示图片")
]
public string vPicture
{
set{vpicture=value;}
get{return vpicture;}
}

private void mybutton_Click(object sender, System.EventArgs e)
{

if(!fileUpload.Value.ToString().Equals(""))
{
LogoURL=fileUpload.PostedFile.FileName.ToString();

LogoURL=LogoURL.Substring(LogoURL.LastIndexOf("."),(LogoURL.Length-LogoURL.LastIndexOf(".")));
if(fileUpload.PostedFile.ContentLength>filelength)
{
myScript("图片超过指定大小!");

}
else
{
if(LogoURL.Equals(".jpg") || LogoURL.Equals(".bmp") || LogoURL.Equals(".gif"))
{

LogoURL=mydirectory+"\\"+imageUrl+LogoURL;
mydirectory=Page.Server.MapPath(" ")+"\\"+mydirectory;


if(Directory.Exists(mydirectory))
{
}
else
{
Directory.CreateDirectory(mydirectory);
}
fileUpload.PostedFile.SaveAs(Page.Server.MapPath(" ")+"\\"+LogoURL);




if(vpicture.Equals("1"))
{
Label1.Text="<img width='100' heigth='100' src='"+LogoURL+"'>";
}

myScript("图片上传成功!");

}
else
{
myScript("文件类型不对!");

}

}
}

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

}

protected override void CreateChildControls()
{
mybutton=new Button();
fileUpload=new HtmlInputFile();
Label1=new Label();
mybutton.Text="提交";

this.Controls.Add(fileUpload);
this.Controls.Add(mybutton);
this.Controls.Add(new LiteralControl("<p>"));
this.Controls.Add(Label1);
this.Controls.Add(new LiteralControl("</p>"));
mybutton.Click+=new EventHandler(mybutton_Click);

}
}
}

收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐