<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>夜半难眠 &#187; 上传组件</title>
	<atom:link href="http://www.havenliu.com/tag/%e4%b8%8a%e4%bc%a0%e7%bb%84%e4%bb%b6/feed" rel="self" type="application/rss+xml" />
	<link>http://www.havenliu.com</link>
	<description>记录点生活的无奈</description>
	<lastBuildDate>Thu, 29 Dec 2011 03:25:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>自己写的一个Flex3多文件上传组件</title>
		<link>http://www.havenliu.com/web/327.html</link>
		<comments>http://www.havenliu.com/web/327.html#comments</comments>
		<pubDate>Wed, 31 Mar 2010 07:43:20 +0000</pubDate>
		<dc:creator>Haven</dc:creator>
				<category><![CDATA[Web开发]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[上传组件]]></category>

		<guid isPermaLink="false">http://www.havenliu.com/?p=327</guid>
		<description><![CDATA[最近在看《Flex3权威指南》，除了书中个别地方翻译的笔误外，确实是本好书，看的兴起，就写了这个多文件上传组件。
网上现成上传组件很多，有些写的很好，我的当然不能和别人那些牛人的相比，仅做学习交流之用，在代码组织和结构设计方面还有很多需要改进的地方，希望大家多提意见。
废话不多说，先看工作界面吧：

我的开发环境是Eclipse+Flex插件+Myeclipse（Myeclipse可有可无，这是我用来开发J2EE的），用MyEclipse7.5集成包的同志们安装Flex插件有困难的可以参考我前面的一篇文章：MyEclipse 7.5,MyEclipse 8.0中安装Flex插件。整个源文件的结构如下：

其实只要看flex_src和src两个目录就可以了。下面主要介绍下程序代码（至于怎样新建Flex Project，怎样下一步之类的就不再罗嗦了，实在不知道的自己Google去）：flex_src 目录下是所有的Flex源代码，src下面的是后台的接收代码（java 的servlet）。com.havenliu.event.DeleteEvent.as是一个自定义的Flex事件类，用来删除单个附件。cpt目录下的deletebtn.mxml是一个自定义删除组件，很简单，里面就一个删除按钮，但附件完成上传后，改按钮变成灰色；最主要的东西都在MyUpload.mxml文件中。

?Download MyUpload.mxml1
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
&#160;
&#60;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34;?&#62;
&#60;mx:Application xmlns:mx=&#34;http://www.adobe.com/2006/mxml&#34; layout=&#34;vertical&#34; fontSize=&#34;13&#34; creationComplete=&#34;init()&#34; xmlns:e=&#34;cpt.*&#34; &#62;
&#60;mx:Script&#62;
	&#60;!&#91;CDATA&#91;
		import com.havenliu.event.DeleteEvent;
		import cpt.deletebtn;
		import mx.events.CloseEvent;
		import mx.collections.ArrayCollection;
		import mx.controls.Alert;
		import flash.profiler.showRedrawRegions;
        import mx.controls.ProgressBar;
&#160;
        //最大上传文件数
		private var maxFiles:int=5;
		//最大上传文件大小(M)
		private var maxSize:Number=50;
		public var fileList:FileReferenceList = new FileReferenceList&#40;&#41;;
&#160;
		private var fileSize:Number=0;
		//后台接受附件的地址，这里是一个servlet，你可以自己定义为jsp、php、asp、action等
		private var uploadurl:URLRequest = new URLRequest&#40;&#34;http://localhost:8080/MyUpload/UploadFile&#34;&#41;;
		&#91;Bindable&#93;
		private var [...]]]></description>
			<content:encoded><![CDATA[<p>最近在看《Flex3权威指南》，除了书中个别地方翻译的笔误外，确实是本好书，看的兴起，就写了这个多文件上传组件。</p>
<p>网上现成上传组件很多，有些写的很好，我的当然不能和别人那些牛人的相比，仅做学习交流之用，在代码组织和结构设计方面还有很多需要改进的地方，希望大家多提意见。</p>
<p>废话不多说，先看工作界面吧：<img src="http://www.havenliu.com/wp-content/uploads/2010/03/init.jpg" alt="init" title="init" width="522" height="324" class="alignnone size-full wp-image-333" /><img class="alignnone size-full wp-image-336" title="work2" src="http://www.havenliu.com/wp-content/uploads/2010/03/work2.jpg" alt="work2" width="530" height="371" /><img class="alignnone size-full wp-image-335" title="work1" src="http://www.havenliu.com/wp-content/uploads/2010/03/work1.jpg" alt="work1" width="521" height="318" /></p>
<p><span id="more-327"></span></p>
<p>我的开发环境是Eclipse+Flex插件+Myeclipse（Myeclipse可有可无，这是我用来开发J2EE的），用MyEclipse7.5集成包的同志们安装Flex插件有困难的可以参考我前面的一篇文章：<a title="MyEclipse  7.5,MyEclipse 8.0中安装Flex插件" href="../java/280.html">MyEclipse 7.5,MyEclipse 8.0中安装Flex插件</a>。整个源文件的结构如下：</p>
<p><img class="alignnone size-full wp-image-334" title="tree" src="http://www.havenliu.com/wp-content/uploads/2010/03/tree.jpg" alt="tree" width="246" height="503" /></p>
<p>其实只要看flex_src和src两个目录就可以了。下面主要介绍下程序代码（至于怎样新建Flex Project，怎样下一步之类的就不再罗嗦了，实在不知道的自己Google去）：flex_src 目录下是所有的Flex源代码，src下面的是后台的接收代码（java 的servlet）。com.havenliu.event.DeleteEvent.as是一个自定义的Flex事件类，用来删除单个附件。cpt目录下的deletebtn.mxml是一个自定义删除组件，很简单，里面就一个删除按钮，但附件完成上传后，改按钮变成灰色；最主要的东西都在MyUpload.mxml文件中。</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.havenliu.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=327&amp;download=MyUpload.mxml">MyUpload.mxml</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3275"><td class="line_numbers"><pre>1
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
</pre></td><td class="code" id="p327code5"><pre class="actionscript" style="font-family:monospace;">&nbsp;
<span style="color: #66cc66;">&lt;</span>?<span style="color: #0066CC;">xml</span> <span style="color: #0066CC;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>mx:Application xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> layout=<span style="color: #ff0000;">&quot;vertical&quot;</span> fontSize=<span style="color: #ff0000;">&quot;13&quot;</span> creationComplete=<span style="color: #ff0000;">&quot;init()&quot;</span> xmlns:<span style="color: #0066CC;">e</span>=<span style="color: #ff0000;">&quot;cpt.*&quot;</span> <span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>mx:Script<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;!</span><span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span>
		<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">havenliu</span>.<span style="color: #006600;">event</span>.<span style="color: #006600;">DeleteEvent</span>;
		<span style="color: #0066CC;">import</span> cpt.<span style="color: #006600;">deletebtn</span>;
		<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">events</span>.<span style="color: #006600;">CloseEvent</span>;
		<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">collections</span>.<span style="color: #006600;">ArrayCollection</span>;
		<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">controls</span>.<span style="color: #006600;">Alert</span>;
		<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">profiler</span>.<span style="color: #006600;">showRedrawRegions</span>;
        <span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">controls</span>.<span style="color: #006600;">ProgressBar</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">//最大上传文件数</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> maxFiles:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">5</span>;
		<span style="color: #808080; font-style: italic;">//最大上传文件大小(M)</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> maxSize:<span style="color: #0066CC;">Number</span>=<span style="color: #cc66cc;">50</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> fileList:FileReferenceList = <span style="color: #000000; font-weight: bold;">new</span> FileReferenceList<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> fileSize:<span style="color: #0066CC;">Number</span>=<span style="color: #cc66cc;">0</span>;
		<span style="color: #808080; font-style: italic;">//后台接受附件的地址，这里是一个servlet，你可以自己定义为jsp、php、asp、action等</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> uploadurl:URLRequest = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http://localhost:8080/MyUpload/UploadFile&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> files:ArrayCollection=<span style="color: #000000; font-weight: bold;">new</span> ArrayCollection<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//附件选择监听器</span>
			fileList.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">SELECT</span>,selectHandler<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//给“添加”按钮注册监听器</span>
			browse.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,browseHandler<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//删除附件按钮注册监听器</span>
			deleteAll.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,resetHandler<span style="color: #66cc66;">&#41;</span>;
			buttonState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> selectHandler<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> countfiles:<span style="color: #0066CC;">int</span>=files.<span style="color: #0066CC;">length</span>+fileList.<span style="color: #006600;">fileList</span>.<span style="color: #0066CC;">length</span>;
			<span style="color: #000000; font-weight: bold;">var</span> countsize:<span style="color: #0066CC;">Number</span>=<span style="color: #cc66cc;">0</span>;
			<span style="color: #808080; font-style: italic;">//计算总附件大小</span>
			<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> _file:FileReference <span style="color: #b1b100;">in</span> fileList.<span style="color: #006600;">fileList</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				countsize+=_file.<span style="color: #0066CC;">size</span>;
			<span style="color: #66cc66;">&#125;</span>
			countsize+=fileSize;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>countfiles<span style="color: #66cc66;">&gt;</span>maxFiles<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				Alert.<span style="color: #0066CC;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;一次最大只能上传&quot;</span>+maxFiles+<span style="color: #ff0000;">&quot;个附件&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>countsize<span style="color: #66cc66;">&gt;</span>maxSize<span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				Alert.<span style="color: #0066CC;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;附件大小不能超过&quot;</span>+maxSize+<span style="color: #ff0000;">&quot;M&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> file:FileReference <span style="color: #b1b100;">in</span> fileList.<span style="color: #006600;">fileList</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>checkDitto<span style="color: #66cc66;">&#40;</span>file<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
					<span style="color: #66cc66;">&#123;</span>
						files.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span>file<span style="color: #66cc66;">&#41;</span>;
						refreshText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
						buttonState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #66cc66;">&#125;</span>
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> browseHandler<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//这里没有过滤文件类型，即所有文件都可以上传，实际开发中，为了安全考虑可能需要过滤</span>
			<span style="color: #000000; font-weight: bold;">var</span> fileType:FileFilter=<span style="color: #000000; font-weight: bold;">new</span> FileFilter<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;文件类型(*.*)&quot;</span>,<span style="color: #ff0000;">&quot;*.*&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> allTypes:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span>fileType<span style="color: #66cc66;">&#41;</span>;				
			fileList.<span style="color: #006600;">browse</span><span style="color: #66cc66;">&#40;</span>allTypes<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> resetHandler<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			Alert.<span style="color: #0066CC;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;您确定要删除全部附件？&quot;</span>,<span style="color: #ff0000;">&quot;提示信息&quot;</span>,Alert.<span style="color: #006600;">YES</span><span style="color: #66cc66;">|</span>Alert.<span style="color: #006600;">NO</span>,fileGrid,alertHandler,<span style="color: #000000; font-weight: bold;">null</span>,Alert.<span style="color: #006600;">NO</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">function</span> alertHandler<span style="color: #66cc66;">&#40;</span>event:CloseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">detail</span>==Alert.<span style="color: #006600;">YES</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					files.<span style="color: #006600;">removeAll</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
					refreshText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
					buttonState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//更新Text文本信息</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> refreshText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//重新统计附件大小，将原来的信息清0</span>
			fileSize=<span style="color: #cc66cc;">0</span>;
			<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&lt;</span>files.<span style="color: #0066CC;">length</span>;i++<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				fileSize+=FileReference<span style="color: #66cc66;">&#40;</span>files<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">size</span>;
			<span style="color: #66cc66;">&#125;</span>
			msg.<span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;共计&quot;</span>+files.<span style="color: #0066CC;">length</span>+<span style="color: #ff0000;">&quot;个附件，文件总大小：&quot;</span>+<span style="color: #66cc66;">&#40;</span>fileSize<span style="color: #66cc66;">/</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">slice</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>+<span style="color: #ff0000;">&quot;M (最大上传5个附件，总大小不超过50M)&quot;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//控制按钮状态</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> buttonState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>files.<span style="color: #006600;">length</span><span style="color: #66cc66;">&lt;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				upload.<span style="color: #0066CC;">enabled</span>=<span style="color: #000000; font-weight: bold;">false</span>;
				deleteAll.<span style="color: #0066CC;">enabled</span>=<span style="color: #000000; font-weight: bold;">false</span>;
			<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span>
			<span style="color: #66cc66;">&#123;</span>
				upload.<span style="color: #0066CC;">enabled</span>=<span style="color: #000000; font-weight: bold;">true</span>;
				deleteAll.<span style="color: #0066CC;">enabled</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>files.<span style="color: #006600;">length</span><span style="color: #66cc66;">&gt;</span>=maxFiles<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				browse.<span style="color: #0066CC;">enabled</span>=<span style="color: #000000; font-weight: bold;">false</span>;
			<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span>
			<span style="color: #66cc66;">&#123;</span>
				browse.<span style="color: #0066CC;">enabled</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> uploadfile<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>files.<span style="color: #006600;">length</span><span style="color: #66cc66;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&lt;</span>files.<span style="color: #0066CC;">length</span>;i++<span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					<span style="color: #000000; font-weight: bold;">var</span> file:FileReference=files<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> as FileReference;
					<span style="color: #0066CC;">try</span><span style="color: #66cc66;">&#123;</span>
						file.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,uploadok<span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//						 file.addEventListener(ProgressEvent.PROGRESS,uploadstart);</span>
						file.<span style="color: #006600;">upload</span><span style="color: #66cc66;">&#40;</span>uploadurl<span style="color: #66cc66;">&#41;</span>;
					<span style="color: #66cc66;">&#125;</span><span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">error</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
					<span style="color: #66cc66;">&#123;</span>
						Alert.<span style="color: #0066CC;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;上传失败&quot;</span>,<span style="color: #0066CC;">error</span>.<span style="color: #0066CC;">message</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #66cc66;">&#125;</span>
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #808080; font-style: italic;">//文件全部上传完成后，上传和全部删除按钮变为灰色</span>
				upload.<span style="color: #0066CC;">enabled</span>=<span style="color: #000000; font-weight: bold;">false</span>;
				deleteAll.<span style="color: #0066CC;">enabled</span>=<span style="color: #000000; font-weight: bold;">false</span>;
				browse.<span style="color: #0066CC;">enabled</span>=<span style="color: #000000; font-weight: bold;">false</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//判断文件是否重复</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkDitto<span style="color: #66cc66;">&#40;</span>file:FileReference<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> result:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">false</span>;
			<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&lt;</span>files.<span style="color: #0066CC;">length</span>;i++<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">var</span> _file:FileReference=files<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
				<span style="color: #808080; font-style: italic;">//如果选择文件的名字、大小和最后修改时间完全一样，则这两个文件重复</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>file.<span style="color: #0066CC;">name</span>==_file.<span style="color: #0066CC;">name</span><span style="color: #66cc66;">||</span>file.<span style="color: #006600;">modificationDate</span>==_file.<span style="color: #006600;">modificationDate</span><span style="color: #66cc66;">||</span>file.<span style="color: #0066CC;">size</span>==_file.<span style="color: #0066CC;">size</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					result= <span style="color: #000000; font-weight: bold;">true</span>;
					<span style="color: #b1b100;">break</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #b1b100;">else</span>
				<span style="color: #66cc66;">&#123;</span>
					result= <span style="color: #000000; font-weight: bold;">false</span>;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">return</span> result;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span>  <span style="color: #000000; font-weight: bold;">function</span> deleteFile<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> indexNo:<span style="color: #0066CC;">int</span>=files.<span style="color: #006600;">getItemIndex</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span> as FileReference<span style="color: #66cc66;">&#41;</span>;
			files.<span style="color: #006600;">removeItemAt</span><span style="color: #66cc66;">&#40;</span>indexNo<span style="color: #66cc66;">&#41;</span>;
			refreshText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			buttonState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span>  <span style="color: #000000; font-weight: bold;">function</span> probar<span style="color: #66cc66;">&#40;</span>event:Event,<span style="color: #0066CC;">data</span>:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> probar:ProgressBar=event.<span style="color: #0066CC;">target</span> as ProgressBar;
			probar.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;%3%%&quot;</span>;
            probar.<span style="color: #006600;">setProgress</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #000000; font-weight: bold;">var</span> file:FileReference=<span style="color: #0066CC;">data</span> as FileReference;
            file.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>ProgressEvent.<span style="color: #006600;">PROGRESS</span>,
            	<span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>event: ProgressEvent<span style="color: #66cc66;">&#41;</span>: <span style="color: #0066CC;">void</span>
	                 <span style="color: #66cc66;">&#123;</span>
	                     probar.<span style="color: #006600;">setProgress</span><span style="color: #66cc66;">&#40;</span>event.<span style="color: #0066CC;">bytesLoaded</span>, event.<span style="color: #0066CC;">bytesTotal</span><span style="color: #66cc66;">&#41;</span>;
	                <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> uploadok<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> file: FileReference = event.<span style="color: #0066CC;">target</span> as FileReference;
			file.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,uploadok<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;上载完成&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">//		private function uploadstart(event:Event):void</span>
<span style="color: #808080; font-style: italic;">//		{</span>
<span style="color: #808080; font-style: italic;">//			trace(&quot;上载开始&quot;);</span>
<span style="color: #808080; font-style: italic;">//		}</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> deletefile<span style="color: #66cc66;">&#40;</span>event:DeleteEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> delfile:FileReference=event.<span style="color: #006600;">file</span>;
			deleteFile<span style="color: #66cc66;">&#40;</span>delfile<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>mx:Script<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>mx:Panel <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;500&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;300&quot;</span> layout=<span style="color: #ff0000;">&quot;absolute&quot;</span> title=<span style="color: #ff0000;">&quot;多文件上传组件&quot;</span><span style="color: #66cc66;">&gt;</span>
			<span style="color: #66cc66;">&lt;</span>mx:VBox <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> x=<span style="color: #ff0000;">&quot;0&quot;</span> y=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #66cc66;">&gt;</span>
					<span style="color: #66cc66;">&lt;</span>mx:DataGrid id=<span style="color: #ff0000;">&quot;fileGrid&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> dataProvider=<span style="color: #ff0000;">&quot;{files}&quot;</span><span style="color: #66cc66;">&gt;</span>
						<span style="color: #66cc66;">&lt;</span>mx:columns<span style="color: #66cc66;">&gt;</span>
							<span style="color: #66cc66;">&lt;</span>mx:DataGridColumn headerText=<span style="color: #ff0000;">&quot;文件名&quot;</span> dataField=<span style="color: #ff0000;">&quot;name&quot;</span> <span style="color: #66cc66;">/&gt;</span>
							<span style="color: #66cc66;">&lt;</span>mx:DataGridColumn <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;80&quot;</span> headerText=<span style="color: #ff0000;">&quot;大小(字节)&quot;</span> dataField=<span style="color: #ff0000;">&quot;size&quot;</span><span style="color: #66cc66;">/&gt;</span>
							<span style="color: #66cc66;">&lt;</span>mx:DataGridColumn <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;120&quot;</span> headerText=<span style="color: #ff0000;">&quot;上传进度&quot;</span> dataField=<span style="color: #ff0000;">&quot;loadRate&quot;</span> sortable=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #66cc66;">&gt;&lt;</span>mx:itemRenderer<span style="color: #66cc66;">&gt;</span>
								<span style="color: #66cc66;">&lt;</span>mx:Component<span style="color: #66cc66;">&gt;</span>
								<span style="color: #66cc66;">&lt;</span>mx:HBox fontSize=<span style="color: #ff0000;">&quot;10&quot;</span> fontWeight=<span style="color: #ff0000;">&quot;normal&quot;</span> fontThickness=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #66cc66;">&gt;</span>
								<span style="color: #66cc66;">&lt;</span>mx:ProgressBar verticalCenter=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> paddingLeft=<span style="color: #ff0000;">&quot;5&quot;</span> paddingRight=<span style="color: #ff0000;">&quot;5&quot;</span>
                                    maximum=<span style="color: #ff0000;">&quot;100&quot;</span> minimum=<span style="color: #ff0000;">&quot;0&quot;</span> labelPlacement=<span style="color: #ff0000;">&quot;center&quot;</span> mode=<span style="color: #ff0000;">&quot;manual&quot;</span>
                                   label=<span style="color: #ff0000;">&quot;%3%%&quot;</span> textAlign=<span style="color: #ff0000;">&quot;left&quot;</span> creationComplete=<span style="color: #ff0000;">&quot;outerDocument.probar(event,data)&quot;</span><span style="color: #66cc66;">/&gt;</span>
                                   <span style="color: #66cc66;">&lt;/</span>mx:HBox<span style="color: #66cc66;">&gt;</span>
								<span style="color: #66cc66;">&lt;/</span>mx:Component<span style="color: #66cc66;">&gt;</span>
								<span style="color: #66cc66;">&lt;/</span>mx:itemRenderer<span style="color: #66cc66;">&gt;</span>
							<span style="color: #66cc66;">&lt;/</span>mx:DataGridColumn<span style="color: #66cc66;">&gt;</span>
							<span style="color: #66cc66;">&lt;</span>mx:DataGridColumn    headerText=<span style="color: #ff0000;">&quot;删除&quot;</span> sortable=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #66cc66;">&gt;</span>
							<span style="color: #66cc66;">&lt;</span>mx:itemRenderer<span style="color: #66cc66;">&gt;</span> 
								<span style="color: #66cc66;">&lt;</span>mx:Component<span style="color: #66cc66;">&gt;</span> 
								<span style="color: #66cc66;">&lt;</span>e:deletebtn  tarfile=<span style="color: #ff0000;">&quot;{data as FileReference}&quot;</span> delFile=<span style="color: #ff0000;">&quot;outerDocument.deletefile(event)&quot;</span><span style="color: #66cc66;">&gt;</span>
								<span style="color: #66cc66;">&lt;/</span><span style="color: #0066CC;">e</span>:deletebtn<span style="color: #66cc66;">&gt;</span>
								<span style="color: #66cc66;">&lt;/</span>mx:Component<span style="color: #66cc66;">&gt;</span> 
								<span style="color: #66cc66;">&lt;/</span>mx:itemRenderer<span style="color: #66cc66;">&gt;</span>
							<span style="color: #66cc66;">&lt;/</span>mx:DataGridColumn<span style="color: #66cc66;">&gt;</span>
						<span style="color: #66cc66;">&lt;/</span>mx:columns<span style="color: #66cc66;">&gt;</span>
					<span style="color: #66cc66;">&lt;/</span>mx:DataGrid<span style="color: #66cc66;">&gt;</span>
					<span style="color: #66cc66;">&lt;</span>mx:HBox <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;39&quot;</span><span style="color: #66cc66;">&gt;</span>
							<span style="color: #66cc66;">&lt;</span>mx:Spacer<span style="color: #66cc66;">/&gt;</span>
							<span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Button</span>  id=<span style="color: #ff0000;">&quot;browse&quot;</span> label=<span style="color: #ff0000;">&quot;添加...&quot;</span> textAlign=<span style="color: #ff0000;">&quot;right&quot;</span><span style="color: #66cc66;">/&gt;</span>
							<span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Button</span> id=<span style="color: #ff0000;">&quot;upload&quot;</span> label=<span style="color: #ff0000;">&quot;上传&quot;</span> click=<span style="color: #ff0000;">&quot;uploadfile()&quot;</span> textAlign=<span style="color: #ff0000;">&quot;right&quot;</span><span style="color: #66cc66;">/&gt;</span>
							<span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Button</span> id=<span style="color: #ff0000;">&quot;deleteAll&quot;</span> label=<span style="color: #ff0000;">&quot;全部删除&quot;</span> textAlign=<span style="color: #ff0000;">&quot;right&quot;</span><span style="color: #66cc66;">/&gt;</span>
					<span style="color: #66cc66;">&lt;/</span>mx:HBox<span style="color: #66cc66;">&gt;</span>
					<span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Text</span> id=<span style="color: #ff0000;">&quot;msg&quot;</span> <span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;共计0个附件，文件总大小：0M (最大上传5个附件，总大小不超过50M)&quot;</span> fontSize=<span style="color: #ff0000;">&quot;12&quot;</span> fontFamily=<span style="color: #ff0000;">&quot;Arial&quot;</span><span style="color: #66cc66;">/&gt;</span>
			<span style="color: #66cc66;">&lt;/</span>mx:VBox<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>mx:Panel<span style="color: #66cc66;">&gt;</span>
&nbsp;
<span style="color: #66cc66;">&lt;/</span>mx:Application<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>DeleteEvent.as的源代码：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.havenliu.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=327&amp;download=DeleteEvent.as">DeleteEvent.as</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3276"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p327code6"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">havenliu</span>.<span style="color: #006600;">event</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">FileReference</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DeleteEvent <span style="color: #0066CC;">extends</span> Event
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//当前要删除的附件</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> file:FileReference;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> DeleteEvent<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>,file:FileReference<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">file</span>=file;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>deletebtn.mxml组件的源代码：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.havenliu.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=327&amp;download=deletebtn.mxml">deletebtn.mxml</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3277"><td class="line_numbers"><pre>1
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
</pre></td><td class="code" id="p327code7"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>?<span style="color: #0066CC;">xml</span> <span style="color: #0066CC;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>mx:Canvas xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> creationComplete=<span style="color: #ff0000;">&quot;btnlis()&quot;</span><span style="color: #66cc66;">&gt;</span>
 <span style="color: #66cc66;">&lt;</span>mx:Metadata<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&#91;</span>Event<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;delFile&quot;</span>, <span style="color: #0066CC;">type</span>=<span style="color: #ff0000;">&quot;com.havenliu.event.DeleteEvent&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
 <span style="color: #66cc66;">&lt;/</span>mx:Metadata<span style="color: #66cc66;">&gt;</span>
&nbsp;
<span style="color: #66cc66;">&lt;</span>mx:Script<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;!</span><span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span>
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">havenliu</span>.<span style="color: #006600;">event</span>.<span style="color: #006600;">DeleteEvent</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> tarfile:FileReference;
&nbsp;
		internal <span style="color: #000000; font-weight: bold;">function</span> deleteFile<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> delevent:DeleteEvent=<span style="color: #000000; font-weight: bold;">new</span> DeleteEvent<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;delFile&quot;</span>,tarfile<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">dispatchEvent</span><span style="color: #66cc66;">&#40;</span>delevent<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span> 
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> comlis<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			deletebutton.<span style="color: #0066CC;">enabled</span>=<span style="color: #000000; font-weight: bold;">false</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> btnlis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//给当前附件注册监听器，如果附件已经上传完成，则将删除按钮的enabled设置为false(不可操作)</span>
			tarfile.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,comlis<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>mx:Script<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Button</span> id=<span style="color: #ff0000;">&quot;deletebutton&quot;</span> label=<span style="color: #ff0000;">&quot;删除&quot;</span> click=<span style="color: #ff0000;">&quot;deleteFile()&quot;</span> <span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>mx:Button<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>mx:Canvas<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>后台接收文件的源代码。这里用的是java做的后台。用Servlet接收的。你可以用php或者asp做后台都无所谓，选择自己熟悉的就行：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.havenliu.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=327&amp;download=UploadFile.java">UploadFile.java</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3278"><td class="line_numbers"><pre>1
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
</pre></td><td class="code" id="p327code8"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.havenliu.servlet</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Iterator</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServlet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.fileupload.FileItem</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.fileupload.FileUploadException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.fileupload.disk.DiskFileItemFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.fileupload.servlet.ServletFileUpload</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UploadFile <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>HttpServletRequest request, HttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IOException</span></a> <span style="color: #009900;">&#123;</span>
&nbsp;
		doPost<span style="color: #009900;">&#40;</span>request,response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doPost<span style="color: #009900;">&#40;</span>HttpServletRequest request, HttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IOException</span></a> <span style="color: #009900;">&#123;</span>
&nbsp;
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> tempDir<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;C:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//文件存储路径</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> saveDir<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;C:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>save<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//临时文件存储路径</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a><span style="color: #009900;">&#40;</span>saveDir<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isDirectory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a><span style="color: #009900;">&#40;</span>saveDir<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">mkdirs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a><span style="color: #009900;">&#40;</span>tempDir<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isDirectory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a><span style="color: #009900;">&#40;</span>tempDir<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">mkdirs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		DiskFileItemFactory dfif <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DiskFileItemFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		dfif.<span style="color: #006633;">setSizeThreshold</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4096</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		dfif.<span style="color: #006633;">setRepository</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a><span style="color: #009900;">&#40;</span>tempDir<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		ServletFileUpload sfu <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ServletFileUpload<span style="color: #009900;">&#40;</span>dfif<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		sfu.<span style="color: #006633;">setHeaderEncoding</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;utf-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 设置最大上传尺寸</span>
		sfu.<span style="color: #006633;">setSizeMax</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">50</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1024</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// 从request得到 所有 上传域的列表</span>
		List<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span> fileList <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			fileList <span style="color: #339933;">=</span> sfu.<span style="color: #006633;">parseRequest</span><span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>FileUploadException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">// 处理文件尺寸过大异常</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fileList <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> fileList.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
		<span style="color: #666666; font-style: italic;">// 得到所有上传的文件</span>
		Iterator<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span> fileItr <span style="color: #339933;">=</span> fileList.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>fileItr.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			 FileItem item <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>FileItem<span style="color: #009900;">&#41;</span> fileItr.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
             <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>item.<span style="color: #006633;">isFormField</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                 <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name <span style="color: #339933;">=</span> item.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                     item.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a><span style="color: #009900;">&#40;</span>saveDir <span style="color: #339933;">+</span> name<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                     e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 <span style="color: #009900;">&#125;</span>
             <span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>最后别忘了在web.xml中注册你的servlet。<br />
到这里基本就差不多了，代码中的注释基本能看懂了。人品不是很差的话应该能直接跑起来了。源代码包就不放上来了，主要是没地方上传，那些网盘什么的我不太喜欢用。需要的朋友们留下邮箱我发给你们。</p>
<p><span style="color: #ff6600;">PS：最后叽歪两句：现在找个满意的图床真难啊，国内的基本不敢用，很多不稳定，开始还好，最后那天就悄然无声的关服务器了，让你哭都哭不出来。稳定的很多又不支持外链，类似163之类的；那些支持的外链、也勉强能算的上稳定的又有很多莫名奇妙的规矩，限制大小和流量啊（这还好，还勉强能接受），自做主张的给你的图片加水印（最不能接收这点）什么。国外的好的图床到是蛮多，比如google的picasa 、雅虎的flickr什么的，这些都用过，但最后都在伟大的GFW下壮烈牺牲了，就算有些坚持到现在还能用，但谁也不敢保证明天早上我打开的我的blog不看见红叉叉。犹豫再三。国外的也不敢用了。。。。。。在折腾了两天后，终于决定把文章里的图片就放在我自己的空间里，从长远来考虑这不太好，但这也是目前最好的选择了。。。。。。。</span></p>
<div style="float:left;margin:0px 0px 0px 0px;"><a title="Post on Google Buzz" class="google-buzz-button" href="http://www.google.com/buzz/post" data-button-style="small-count" data-url="http://www.havenliu.com/web/327.html"></a><script type="text/javascript" src="http://www.google.com/buzz/api/button.js"></script></div><h3  class="related_post_title">相关文章</h3><ul class="related_post"><li>2009年12月10日 -- <a href="http://www.havenliu.com/java/280.html" title="MyEclipse 7.5,MyEclipse 8.0中安装Flex插件">MyEclipse 7.5,MyEclipse 8.0中安装Flex插件</a></li><li>2009年08月14日 -- <a href="http://www.havenliu.com/java/191.html" title="发贴器有了比较大的进展">发贴器有了比较大的进展</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.havenliu.com/web/327.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

