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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155
