一个简单的文件上传功能控件
1
using System;
2
using System.Web.UI;
3
using System.Web.UI.WebControls;
4
using System.ComponentModel;
5
using System.Web.UI.HtmlControls;
6
using System.IO;
7
8
namespace 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
using System;2
using System.Web.UI;3
using System.Web.UI.WebControls;4
using System.ComponentModel;5
using System.Web.UI.HtmlControls;6
using System.IO;7

8
namespace UpLoadImage9
{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 FileLength38
{39
set{filelength=value;}40
get{return filelength;}41
}42
43
[44
DescriptionAttribute("图片名字")45
]46
public string ImageUrl47
{48
set{imageUrl=value;}49
get{return imageUrl;}50
}51
52
[53
DescriptionAttribute("文件路径")54
]55
public string MyDirectory56
{57
get{return mydirectory;}58
set{mydirectory=value;}59
}60
61
[62
DescriptionAttribute("图片的相对地址")63
]64
public string Logo65
{66
67
get{return LogoURL;}68
set{LogoURL=value;}69
}70
71
[72
DescriptionAttribute("是否显示图片")73
]74
public string vPicture75
{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
else94
{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
else106
{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
else123
{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


浙公网安备 33010602011771号