using system;
using system.web.ui;
using system.web.ui.webcontrols;
using system.componentmodel;
using system.web.ui.htmlcontrols;
using system.io;
using system.drawing;
using system.drawing.design;
namespace yingnet.common
{
/// <summary>
/// fileupload 的摘要说明。e:\program\common\fileupload.bmp
/// </summary>
[toolboxbitmap(typeof(yingnet.common.fileupload), "fileupload.bmp"),
defaultproperty("text"), defaultevent("click"),
toolboxdata("<{0}:fileupload runat=server></{0}:fileupload>")]
public class fileupload : system.web.ui.webcontrols.webcontrol {
/// <summary>
/// 上传按钮
/// </summary>
private button button=new button();
/// <summary>
/// 上传文件个数
/// </summary>
private int filenum=1;
/// <summary>
/// file对象
/// </summary>
private htmlinputfile[] file;
/// <summary>
/// 保存路径,默认为系统的临时目录
/// </summary>
private string path=system.io.path.gettemppath();
/// <summary>
/// 上传的文件名组
/// </summary>
private string[] filename;
/// <summary>
/// 后缀文件名组
/// </summary>
private string[] suffix;
/// <summary>
///过滤器,写法是.txt;.abc
/// </summary>
private string filter="";
/// <summary>
/// 限制文件上传大小,为0是不限制,单位是字节
/// </summary>
private int size=0;//system.componentmodel.defaulteventattribute
/// <summary>
/// 上传事件
/// </summary>
[bindable(true),category("事件"),description("上传后激发的事件")
]
public event eventhandler click;
/// <summary>
/// 上传文件数
/// </summary>
[bindable(true),
category("参数"),description("设定上传文件的个数"),
defaultvalue("1")]
public int filenum{
set{
if(value<1){
value=1;
}
filenum=value;
this.controls.clear();
file=new htmlinputfile[filenum];
filename=new string[filenum];
suffix=new string[filenum];
for(int i=0;i<filenum;i++) {
file[i]=new htmlinputfile();
this.controls.add(file[i]);
}
this.controls.add(button);
}
get{
return filenum;
}
}
/// <summary>
/// 上传按钮的文本
/// </summary>
[bindable(true),
category("参数"), description("设定上传文件的路径"),
defaultvalue("1")]
/// <summary>
/// 上传路径
/// </summary>
public string uploadpath {
set{
if("".equals(value)||value==null){
value=system.io.path.gettemppath();
}
path=value;
}
get{
return path;
}
}
/// <summary>
/// 得到文件名
/// </summary>
public string[] filename{
get{
return filename;
}
}
/// <summary>
/// 得到后缀
/// </summary>
public string[] suffix{
get{
return suffix;
}
}
/// <summary>
/// 过滤器
/// </summary>
public string filter{
set{
filter=value;
}
get{
return filter;
}
}
/// <summary>
/// 限制大小
/// </summary>
public int filesize{
set{
size=value;
}
get{
return size;
}
}
/// <summary>
/// 快捷键
/// </summary>
public override string accesskey{
get{
return button.accesskey;
}
set{
button.accesskey=value;
}
}
/// <summary>
/// 背景
/// </summary>
public override system.drawing.color backcolor{
get{
return button.backcolor;
}
set{
button.backcolor=value;
}
}
/// <summary>
/// 边框颜色
/// </summary>
public override system.drawing.color bordercolor{
get{
return button.bordercolor;
}
set{
button.bordercolor=value;
}
}
/// <summary>
/// 边框风格
/// </summary>
public override borderstyle borderstyle{
get{
return button.borderstyle;
}
set{
button.borderstyle=value;
}
}
/// <summary>
/// 文本
/// </summary>
[bindable(true),
category("appearance"),
defaultvalue("")]
public string text
{
get
{
return button.text;
}
set
{
button.text = value;
}
}
public fileupload():base(){
filenum=1;
button.click+=new eventhandler(this.button_click);
this.controls.add(button);
}
/// <summary>
/// 按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_click(object sender, eventargs e){
upload();
///添加你的代码
if(click!=null)
click(sender,e); ///抛处事件
}
/// <summary>
/// 上传
/// </summary>
private void upload(){
system.web.httppostedfile postedfile;
for(int i=0;i<filenum;i++){
try{
postedfile=file[i].postedfile;
if(postedfile!=null) {
if(postedfile.contentlength>size && size!=0){
break;
}
string suf=getsuffix(postedfile.filename);
if(filter!=null && filter.indexof(suf)>-1){
break;
}
filename[i]=datetime.now.ticks.tostring();
suffix[i]=suf;
postedfile.saveas(system.io.path.combine(path,filename[i]+suf));
}
}finally{
filename[i]="";
}
}
}
/// <summary>
/// 获取后缀名
/// </summary>
/// <param name="filename">文件名</param>
/// <returns>返回带.的后缀名</returns>
private string getsuffix(string filename){
int index=filename.lastindexof(".");
if(index>0){
return filename.substring(index);
}
return "";
}
}
}
我自己写的自定义Web的上传控件-.NET教程,Asp.Net开发
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 我自己写的自定义Web的上传控件-.NET教程,Asp.Net开发
相关推荐
- 暂无文章
