自定义服务器控件开发之2:文件上传控件

        文件上传的控件,支持上传后事件,跨服务器上传,自定义可上传文件类型、大小,以及上传文件命名规则。

  1 None.gif using  System.Web.UI.HtmlControls;
  2 None.gif using  System.Configuration;
  3 None.gif using  System.ComponentModel;
  4 None.gif using  System.Web.Security;
  5 None.gif using  System.Security.Principal;
  6 None.gif using  System.Drawing;
  7 None.gif using  System.Drawing.Text;
  8 None.gif using  System.Collections;
  9 None.gif using  System.Collections.Specialized;
 10 None.gif
 11 None.gif namespace  Rungoo.WebCtrlLib
 12 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 13ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 14InBlock.gif    /// 文件上传控件
 15InBlock.gif    /// Author:    nowind
 16InBlock.gif    /// Date:    2006-3-4
 17InBlock.gif    /// Email:    hgh113@sina.com
 18InBlock.gif    /// QQ:        87505959
 19ExpandedSubBlockEnd.gif    /// </summary>

 20InBlock.gif    [DefaultProperty("Text"), 
 21InBlock.gif    ToolboxData("<{0}:WebUploadFile runat=server></{0}:WebUploadFile>")]
 22InBlock.gif    public class WebUploadFile : System.Web.UI.WebControls.WebControl,INamingContainer
 23ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 24InBlock.gif        private event EventHandler uploaded;
 25InBlock.gif        
 26ContractedSubBlock.gifExpandedSubBlockStart.gif        属性#region 属性
 27InBlock.gif
 28ContractedSubBlock.gifExpandedSubBlockStart.gif        机器属性#region 机器属性
 29ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 30InBlock.gif        /// 域
 31ExpandedSubBlockEnd.gif        /// </summary>

 32InBlock.gif        [Bindable(true), 
 33InBlock.gif        Category("Data"),Description("图片存放目标计算机的IP或者机器名,只在跨计算机上传时候填写"),
 34InBlock.gif        DefaultValue("")] 
 35InBlock.gif        public string ComputerDomain
 36ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 37ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ViewState["ComputerDomain"== null ? String.Empty : (string)ViewState["ComputerDomain"] ; }
 38ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ ViewState["ComputerDomain"= value; }
 39ExpandedSubBlockEnd.gif        }

 40InBlock.gif
 41ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 42InBlock.gif        /// 登陆用户
 43ExpandedSubBlockEnd.gif        /// </summary>

 44InBlock.gif        [Bindable(true), 
 45InBlock.gif        Category("Data"),Description("图片存放目标计算机登陆用户,只在跨计算机上传时候填写"),
 46InBlock.gif        DefaultValue("")] 
 47InBlock.gif        public string ComputerUsername
 48ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 49ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ViewState["ComputerUsername"== null ? String.Empty : (string)ViewState["ComputerUsername"] ; }
 50ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ ViewState["ComputerUsername"= value; }
 51ExpandedSubBlockEnd.gif        }

 52InBlock.gif
 53ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 54InBlock.gif        /// 登陆密码
 55ExpandedSubBlockEnd.gif        /// </summary>

 56InBlock.gif        [Bindable(true), 
 57InBlock.gif        Category("Data"),Description("图片存放目标计算机登陆密码,只在跨计算机上传时候填写"),
 58InBlock.gif        DefaultValue("")] 
 59InBlock.gif        public string ComputerPassword
 60ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 61ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ViewState["ComputerPassword"== null ? String.Empty : (string)ViewState["ComputerPassword"] ; }
 62ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ ViewState["ComputerPassword"= value; }
 63ExpandedSubBlockEnd.gif        }

 64InBlock.gif
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 66InBlock.gif        /// 文件存放路径
 67ExpandedSubBlockEnd.gif        /// </summary>

 68InBlock.gif        [Bindable(true), 
 69InBlock.gif        Category("Data"),Description(@"文件存放的物理路径。如果跨计算机上传,则必须填写,且包含完整共享目录路径,且保证该目录Windows权限可写,如 \\192.168.3.1\nowind\upload\;如果上传本地机器,则为图片存放物理目录全路径,如 D:\nowind\113\ ,也可以不填写,自动采用 ComputerUrlPath 指定的目录"),
 70InBlock.gif        DefaultValue("")] 
 71InBlock.gif        public string ComputerFilePath
 72ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 73ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ViewState["ComputerFilePath"== null ? "" : (string)ViewState["ComputerFilePath"] ; }
 74ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ ViewState["ComputerFilePath"= value;  }
 75ExpandedSubBlockEnd.gif        }

 76InBlock.gif
 77ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 78InBlock.gif        /// 文件服务路径
 79ExpandedSubBlockEnd.gif        /// </summary>

 80InBlock.gif        [Bindable(true), 
 81InBlock.gif        Category("Data"),Description(@"文件的Web访问路径。跨计算机上传时,必须填写Url完全路径,如 http://192.168.3.1/nowind/upload/ ;如果上传本地机器,则填写站点根目录后的全路径,如upload/file/"),
 82InBlock.gif        DefaultValue("")] 
 83InBlock.gif        public string ComputerUrlPath
 84ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 85ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ViewState["ComputerUrlPath"== null ? String.Empty : (string)ViewState["ComputerUrlPath"] ; }
 86InBlock.gif            set 
 87ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 88InBlock.gif                value = value.EndsWith(@"/"? value : value+"/";
 89InBlock.gif                ViewState["ComputerUrlPath"= value; 
 90ExpandedSubBlockEnd.gif            }

 91ExpandedSubBlockEnd.gif        }

 92ExpandedSubBlockEnd.gif        #endregion

 93InBlock.gif
 94ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 95InBlock.gif        /// 上传成功后是否显示文件信息
 96ExpandedSubBlockEnd.gif        /// </summary>

 97InBlock.gif        [Bindable(true), 
 98InBlock.gif        Category("Appearance"),Description("上传成功后是否显示文件信息"),
 99InBlock.gif        DefaultValue("")] 
100InBlock.gif        public System.Boolean IsShowFileInfo
101ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
102ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ViewState["IsShowFileInfo"== null ? false : (bool)ViewState["IsShowFileInfo"] ; }
103ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ ViewState["IsShowFileInfo"= value; }
104ExpandedSubBlockEnd.gif        }

105InBlock.gif
106ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
107InBlock.gif        /// 文件大小上限
108ExpandedSubBlockEnd.gif        /// </summary>

109InBlock.gif        [Bindable(true), 
110InBlock.gif        Category("Data"),Description("文件大小上限,以K为单位,默认为100K,0表示不限制"),
111InBlock.gif        DefaultValue("")] 
112InBlock.gif        public System.Int64 FileMaxSize
113ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
114ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ViewState["FileMaxSize"== null ? 100 : (System.Int64)ViewState["FileMaxSize"] ; }
115InBlock.gif            set 
116ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
117InBlock.gif                if( value < 0 || value > System.Int64.MaxValue)
118InBlock.gif                    throw new ArgumentException("设置的值超过有效范围");
119InBlock.gif                ViewState["FileMaxSize"= value; 
120ExpandedSubBlockEnd.gif            }

121ExpandedSubBlockEnd.gif        }

122InBlock.gif
123ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
124InBlock.gif        /// 是否覆盖服务器上已存在的同名文件
125ExpandedSubBlockEnd.gif        /// </summary>

126InBlock.gif        [Bindable(true), 
127InBlock.gif        Category("Data"),Description("是否覆盖服务器上已存在的同名文件"),
128InBlock.gif        DefaultValue("")] 
129InBlock.gif        public System.Boolean IsOverwrite
130ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
131ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ViewState["IsOverwrite"== null ? true : (bool)ViewState["IsOverwrite"] ; }
132ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ ViewState["IsOverwrite"= value; }
133ExpandedSubBlockEnd.gif        }

134InBlock.gif
135ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
136InBlock.gif        /// 是否重新命名上传文件
137ExpandedSubBlockEnd.gif        /// </summary>

138InBlock.gif        [Bindable(true), 
139InBlock.gif        Category("Data"),Description("是否重新命名上传文件"),
140InBlock.gif        DefaultValue("")] 
141InBlock.gif        public System.Boolean IsRename
142ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
143ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ViewState["IsRename"== null ? true : (bool)ViewState["IsRename"] ; }
144ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ ViewState["IsRename"= value; }
145ExpandedSubBlockEnd.gif        }

146InBlock.gif
147ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
148InBlock.gif        /// 文件类型
149ExpandedSubBlockEnd.gif        /// </summary>

150InBlock.gif        [Bindable(true), 
151InBlock.gif        Category("Data"),Description("文件类型,多种文件类型以(,)分割,默认为rar,zip,doc,txt,wma,mp3,pdf类型"),
152InBlock.gif        DefaultValue("")] 
153InBlock.gif        public System.String FileType
154ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
155ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ViewState["FileType"== null ? "rar,zip,doc,txt,wma,mp3,pdf" : (string)ViewState["FileType"] ; }
156ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ ViewState["FileType"= value; }
157ExpandedSubBlockEnd.gif        }

158InBlock.gif
159ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
160InBlock.gif        /// 文件的完整Url路径
161ExpandedSubBlockEnd.gif        /// </summary>

162InBlock.gif        [Bindable(true), 
163InBlock.gif        Category("Data"),Description("文件的完整Url路径,相对于根目录,如image/xxx.jpg"),Browsable(false),
164InBlock.gif        DefaultValue("")] 
165InBlock.gif        public System.String FileFullUrl
166ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
167ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ViewState["FileFullUrl"== null ? String.Empty : (string)ViewState["FileFullUrl"] ; }
168ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ ViewState["FileFullUrl"= value; }
169ExpandedSubBlockEnd.gif        }

170InBlock.gif        
171ContractedSubBlock.gifExpandedSubBlockStart.gif        其他属性#region 其他属性
172InBlock.gif
173InBlock.gif        //控件宽度属性 
174InBlock.gif        [Bindable(true), 
175InBlock.gif        Category("Layout"), 
176InBlock.gif        DefaultValue("")] 
177InBlock.gif        public override Unit Width
178ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
179ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gifreturn this.pnlFrame.Width;}
180ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gifthis.pnlFrame.Width = value;}
181ExpandedSubBlockEnd.gif        }

182InBlock.gif
183InBlock.gif        //控件高度属性 
184InBlock.gif        [Bindable(true), 
185InBlock.gif        Category("Layout"), 
186InBlock.gif        DefaultValue("")] 
187InBlock.gif        public override Unit Height
188ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
189ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gifreturn this.pnlFrame.Height;}
190ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gifthis.pnlFrame.Height = value;}
191ExpandedSubBlockEnd.gif        }

192InBlock.gif
193InBlock.gif        //前景色属性 
194InBlock.gif        [Bindable(true), 
195InBlock.gif        Category("Appearance"), 
196InBlock.gif        DefaultValue("")] 
197InBlock.gif        public override Color ForeColor
198ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
199ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gifreturn pnlFrame.ForeColor;}
200ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gifthis.pnlFrame.ForeColor = value;}
201ExpandedSubBlockEnd.gif        }

202InBlock.gif
203InBlock.gif        //背景色属性 
204InBlock.gif        [Bindable(true), 
205InBlock.gif        Category("Appearance"), 
206InBlock.gif        DefaultValue("")] 
207InBlock.gif        public override Color BackColor
208ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
209ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gifreturn pnlFrame.BackColor;}
210ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gifthis.pnlFrame.BackColor = value;}
211ExpandedSubBlockEnd.gif        }

212InBlock.gif
213InBlock.gif        //按钮样式
214InBlock.gif        [Bindable(true), 
215InBlock.gif        Category("Appearance"),Description("按钮样式"),
216InBlock.gif        DefaultValue("")] 
217InBlock.gif        public string CssButton 
218ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
219ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ViewState["CssButton"== null ? String.Empty : (string)ViewState["CssButton"] ; }
220ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ ViewState["CssButton"= value; }
221ExpandedSubBlockEnd.gif        }

222InBlock.gif
223InBlock.gif        //浏览框样式属性
224InBlock.gif        [Bindable(true), 
225InBlock.gif        Category("Appearance"),Description("浏览框样式属性"),
226InBlock.gif        DefaultValue("")] 
227InBlock.gif        public string CssInputFile
228ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
229ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ViewState["CssInputFile"== null ? String.Empty : (string)ViewState["CssInputFile"] ; }
230ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ ViewState["CssInputFile"= value; }
231ExpandedSubBlockEnd.gif        }

232InBlock.gif
233InBlock.gif        //文字样式属性
234InBlock.gif        [Bindable(true), 
235InBlock.gif        Category("Appearance"),Description("文字样式属性"),
236InBlock.gif        DefaultValue("")] 
237InBlock.gif        public override string CssClass 
238ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
239ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ViewState["CssClass"== null ? String.Empty : (string)ViewState["CssClass"] ; }
240ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ ViewState["CssClass"= value; }
241ExpandedSubBlockEnd.gif        }

242InBlock.gif
243InBlock.gif        //控件边框颜色属性 
244InBlock.gif        [Bindable(true), 
245InBlock.gif        Category("Appearance"), 
246InBlock.gif        DefaultValue("")] 
247InBlock.gif        public override Color BorderColor
248ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
249ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gifreturn this.pnlFrame.BorderColor;}
250ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gifthis.pnlFrame.BorderColor = value;}
251ExpandedSubBlockEnd.gif        }

252InBlock.gif
253InBlock.gif        //控件边框样式属性 
254InBlock.gif        [Bindable(true), 
255InBlock.gif        Category("Appearance"), 
256InBlock.gif        DefaultValue("")] 
257InBlock.gif        public override BorderStyle BorderStyle
258ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
259ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gifreturn this.pnlFrame.BorderStyle;}
260ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gifthis.pnlFrame.BorderStyle = value;}
261ExpandedSubBlockEnd.gif        }

262InBlock.gif
263InBlock.gif        //控件边框宽度属性 
264InBlock.gif        [Bindable(true), 
265InBlock.gif        Category("Appearance"), 
266InBlock.gif        DefaultValue("")] 
267InBlock.gif        public override Unit BorderWidth
268ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
269ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gifreturn this.pnlFrame.BorderWidth;}
270ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gifthis.pnlFrame.BorderWidth = value;}
271ExpandedSubBlockEnd.gif        }

272InBlock.gif
273ExpandedSubBlockEnd.gif        #endregion

274InBlock.gif
275InBlock.gif        //处理上传成功后用户自定义事件
276InBlock.gif        [Bindable(true), 
277InBlock.gif        Category("Action"), Description("处理上传成功后用户自定义事件"),
278InBlock.gif        DefaultValue("")] 
279InBlock.gif        public event EventHandler Uploaded
280ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
281ExpandedSubBlockStart.gifContractedSubBlock.gif            adddot.gifthis.uploaded += value;}
282ExpandedSubBlockStart.gifContractedSubBlock.gif            removedot.gifthis.uploaded -= value;}
283ExpandedSubBlockEnd.gif        }

284ExpandedSubBlockEnd.gif        #endregion

285InBlock.gif
286ContractedSubBlock.gifExpandedSubBlockStart.gif        声明子控件#region 声明子控件
287InBlock.gif        private Label lblInfo = new Label();                //用于文件信息
288InBlock.gif        private Label lblMsg = new Label();                //上传文件重命名称
289InBlock.gif        private System.Web.UI.WebControls.FileUpload fileUpload = new FileUpload();        //浏览本地文件
290InBlock.gif        private System.Web.UI.HtmlControls.HtmlButton btnUpload = new HtmlButton();        //上传按钮
291InBlock.gif        private Panel pnlFrame = new Panel();                //承载其它控件的容器Panel控件
292ExpandedSubBlockEnd.gif        #endregion

293InBlock.gif
294ContractedSubBlock.gifExpandedSubBlockStart.gif        构造函数初始化子控件#region    构造函数初始化子控件
295InBlock.gif        public WebUploadFile()
296ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
297InBlock.gif            this.fileUpload.ID = "fileUpload";
298InBlock.gif
299InBlock.gif            this.lblInfo.ID = "lblInfo";
300InBlock.gif            this.lblInfo.Text = this.FileFullUrl;
301InBlock.gif            this.lblInfo.Visible = false;
302InBlock.gif
303InBlock.gif            this.lblMsg.ID = "lblMsg";
304InBlock.gif            this.lblMsg.Text = "";
305InBlock.gif            this.lblMsg.ForeColor = Color.Red;
306InBlock.gif            this.lblMsg.Visible = false;
307InBlock.gif
308InBlock.gif            this.btnUpload.ID = "btnUpload";
309InBlock.gif            this.btnUpload.InnerText = "上传";
310InBlock.gif            this.btnUpload.ServerClick += new EventHandler(this.Upload_Click);
311InBlock.gif
312InBlock.gif            this.pnlFrame.ForeColor = this.ForeColor;
313InBlock.gif            this.pnlFrame.BackColor = Color.Empty;
314InBlock.gif
315InBlock.gif            //将子控件添加到此自定义控件中
316InBlock.gif            this.Controls.Add(fileUpload);
317InBlock.gif            this.Controls.Add(lblInfo);
318InBlock.gif            this.Controls.Add(lblMsg);
319InBlock.gif            this.Controls.Add(btnUpload);
320InBlock.gif            this.Controls.Add(pnlFrame);
321ExpandedSubBlockEnd.gif        }

322InBlock.gif
323ExpandedSubBlockEnd.gif        #endregion

324InBlock.gif
325ContractedSubBlock.gifExpandedSubBlockStart.gif        创建子控件#region 创建子控件
326InBlock.gif        protected override void EnsureChildControls()
327ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
328InBlock.gif            
329ExpandedSubBlockEnd.gif        }

330ExpandedSubBlockEnd.gif        #endregion

331InBlock.gif    
332ContractedSubBlock.gifExpandedSubBlockStart.gif        将此控件呈现给指定的输出参数#region 将此控件呈现给指定的输出参数
333ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
334InBlock.gif        /// 将此控件呈现给指定的输出参数。
335InBlock.gif        /// </summary>
336ExpandedSubBlockEnd.gif        /// <param name="output"> 要写出到的 HTML 编写器 </param>

337InBlock.gif        protected override void Render(HtmlTextWriter output)
338ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
339InBlock.gif            //开始输出
340InBlock.gif            output.AddAttribute(HtmlTextWriterAttribute.Id,base.ID);
341InBlock.gif            this.pnlFrame.RenderBeginTag(output);
342InBlock.gif
343InBlock.gif            //在Panel中绘制表格
344InBlock.gif            output.AddAttribute(HtmlTextWriterAttribute.Border,"0");
345InBlock.gif            output.AddAttribute(HtmlTextWriterAttribute.Cellpadding,"0");
346InBlock.gif            output.AddAttribute(HtmlTextWriterAttribute.Cellspacing,"0");
347InBlock.gif            output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Width,"100%");
348InBlock.gif            //output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Height,"25px");
349InBlock.gif
350InBlock.gif            output.RenderBeginTag(HtmlTextWriterTag.Table); 
351InBlock.gif
352InBlock.gif            output.RenderBeginTag(HtmlTextWriterTag.Tr);
353InBlock.gif            output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align,"Left");    
354InBlock.gif            //output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Width,"70%");
355InBlock.gif            output.RenderBeginTag(HtmlTextWriterTag.Td);
356InBlock.gif            
357InBlock.gif            //文件浏览框
358InBlock.gif            //output.Write("<input name=\"" + base.ID + ":fileUpload\" id=\"" + base.ID + "_fileUpload\" class=\"" + this.CssInputFile + "\" style=\"width:75%\" type=\"file\" />");
359InBlock.gif            this.fileUpload.Attributes.Add("Width""70%");
360InBlock.gif            this.fileUpload.Attributes.Add("Class"this.CssInputFile);
361InBlock.gif            this.fileUpload.RenderControl(output);
362InBlock.gif            output.Write("&nbsp;");
363InBlock.gif            //上传按钮
364InBlock.gif            //this.btnUpload.Attributes.Add("OnClick","javascript:if(document.getElementById('"+this.ID+"_fileUpload').value==''){alert('请浏览上传的文件');return false;}");
365InBlock.gif            this.btnUpload.Attributes.Add("Class"this.CssButton);
366InBlock.gif            this.btnUpload.CausesValidation = false;
367InBlock.gif            this.btnUpload.RenderControl(output);
368InBlock.gif
369InBlock.gif            output.Write("&nbsp;");
370InBlock.gif            //上传结果信息
371InBlock.gif            this.lblMsg.RenderControl(output);
372InBlock.gif            output.RenderEndTag();
373InBlock.gif            output.RenderEndTag();
374InBlock.gif
375InBlock.gif            //显示文件信息
376InBlock.gif            ifthis.IsShowFileInfo == true )
377ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
378InBlock.gif                output.RenderBeginTag(HtmlTextWriterTag.Tr);
379InBlock.gif                output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Valign,"Middle");
380InBlock.gif                output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Height,"24px");
381InBlock.gif                output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Class,this.CssClass);
382InBlock.gif                output.RenderBeginTag(HtmlTextWriterTag.Td);
383InBlock.gif                output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align,"left");
384InBlock.gif                output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Valign,"absmiddle");
385InBlock.gif
386InBlock.gif                this.lblInfo.RenderControl(output);
387InBlock.gif
388InBlock.gif                output.RenderEndTag();// </td>
389InBlock.gif                output.RenderEndTag();// </tr>
390ExpandedSubBlockEnd.gif            }

391InBlock.gif
392InBlock.gif            output.RenderEndTag();// </table>
393InBlock.gif            
394InBlock.gif            //结束输出
395InBlock.gif            this.pnlFrame.RenderEndTag(output);
396ExpandedSubBlockEnd.gif        }

397ExpandedSubBlockEnd.gif        #endregion
    
398InBlock.gif
399ContractedSubBlock.gifExpandedSubBlockStart.gif        上载文件#region 上载文件
400InBlock.gif        private void Upload_Click(object sender, System.EventArgs e)
401ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
402InBlock.gif            this.lblInfo.Visible = false;
403InBlock.gif            //this.lblInfo.Text = "";
404InBlock.gif            this.lblMsg.Visible = false;
405InBlock.gif            this.lblMsg.Text = "";
406InBlock.gif
407InBlock.gif            System.Web.UI.ClientScriptManager client = this.Page.ClientScript;
408InBlock.gif            Type cstype = Page.GetType();
409InBlock.gif
410InBlock.gif            //站点根路径
411InBlock.gif            string appPath = FileClass.GetApplicationPath();
412InBlock.gif
413InBlock.gif            // 判断路径
414InBlock.gif            ifthis.fileUpload.PostedFile == null || this.fileUpload.PostedFile.ToString() == "" )
415ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
416InBlock.gif                //this.lblMsg.Text = "未发现操作文件";
417InBlock.gif                //this.lblMsg.Visible = true;
418InBlock.gif                client.RegisterStartupScript(cstype, "error""<script language='javascript'>alert('未发现操作文件')</script>");
419InBlock.gif                return;
420ExpandedSubBlockEnd.gif            }

421InBlock.gif
422InBlock.gif            FileClass classValidUser = new FileClass();
423InBlock.gif            ifthis.ComputerDomain != "" )    //如果计算机地址设置不为空则默认为存放文件到远程计算机
424ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
425InBlock.gif                bool loginResult = false;    //登陆远程计算机成功与否
426InBlock.gif                loginResult = classValidUser.ImpersonateValidUser(this.ComputerUsername , this.ComputerPassword , this.ComputerDomain );
427InBlock.gif                if( loginResult == false )
428ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
429InBlock.gif                    //lblMsg.Visible = true;
430InBlock.gif                    //lblMsg.Text = "登陆远程服务器失败";
431InBlock.gif                    client.RegisterStartupScript(cstype, "error""<script language='javascript'>alert('登陆远程服务器失败')</script>");
432InBlock.gif                    return;
433ExpandedSubBlockEnd.gif                }

434ExpandedSubBlockEnd.gif            }

435InBlock.gif
436InBlock.gif            try
437ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
438InBlock.gif                string fileName = null;    //保存的文件短名称
439InBlock.gif                HttpPostedFile filImage = this.fileUpload.PostedFile;
440InBlock.gif                string sourceFileName = null;    //原始文件名称    
441InBlock.gif                sourceFileName= System.IO.Path.GetFileName(filImage.FileName);
442InBlock.gif                string sourceFileExtension = System.IO.Path.GetExtension(sourceFileName).ToLower();//扩展名 如.jpg
443InBlock.gif
444InBlock.gif                StringCollection sc = new StringCollection();
445InBlock.gif                foreachstring str in this.FileType.Split(',') )
446InBlock.gif                    sc.Add( "."+str.ToLower() );
447InBlock.gif                if!sc.Contains(sourceFileExtension) )
448ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
449InBlock.gif                    client.RegisterStartupScript(cstype, "error""<script language='javascript'>alert('文件格式不对')</script>");
450InBlock.gif                    return;
451ExpandedSubBlockEnd.gif                }

452InBlock.gif
453InBlock.gif                // 文件大小
454InBlock.gif                int fileLen = filImage.ContentLength; 
455InBlock.gif                ifthis.FileMaxSize != 0 )    //为0则不限制上传文件大小
456ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
457InBlock.gif                    // 判断文件大小
458InBlock.gif                    if(fileLen < 0 || fileLen > this.FileMaxSize * 1024)
459ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
460InBlock.gif                        string msg = "文件大小不能超过"+this.FileMaxSize+"K";
461InBlock.gif                        client.RegisterStartupScript(cstype, "error""<script language='javascript'>alert('" + msg + "')</script>");
462InBlock.gif                        return;
463ExpandedSubBlockEnd.gif                    }

464ExpandedSubBlockEnd.gif                }

465InBlock.gif
466InBlock.gif                // 读取文件到缓存
467InBlock.gif                byte[] bufData = new byte[fileLen];
468InBlock.gif                filImage.InputStream.Read(bufData, 0, fileLen);
469InBlock.gif                MemoryStream ms = new MemoryStream(bufData);
470InBlock.gif                ifthis.IsRename )
471ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
472InBlock.gif                    // 以时间命名文件名,精确到毫秒
473InBlock.gif                    DateTime dtNow = DateTime.Now;
474InBlock.gif                    fileName = dtNow.ToString("yyyyMMddhhmmss")+DateTime.Now.Millisecond.ToString("000")+sourceFileExtension;
475ExpandedSubBlockEnd.gif                }

476InBlock.gif                else
477InBlock.gif                    fileName = sourceFileName;
478InBlock.gif
479InBlock.gif                // 文件存放路径文件夹
480InBlock.gif                string filePath = "";
481InBlock.gif                if(this.ComputerFilePath == null || this.ComputerFilePath == "" )
482ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
483InBlock.gif                    filePath = appPath + this.ComputerUrlPath;
484InBlock.gif                    filePath = filePath.EndsWith(@"/"? filePath : filePath+"/";
485InBlock.gif                    filePath = filePath.Replace(@"//",@"/");
486InBlock.gif                    filePath = this.Page.Server.MapPath( filePath );
487ExpandedSubBlockEnd.gif                }

488InBlock.gif                else
489InBlock.gif                    filePath = this.ComputerFilePath.Replace(@"/",@"\");
490InBlock.gif                filePath = filePath.EndsWith(@"\"== true ? filePath : filePath + @"\";
491InBlock.gif                string fileFullPath = filePath + fileName;
492InBlock.gif
493InBlock.gif                //返回文件url全路径,以"/"结尾
494InBlock.gif                string fileUrlPath = this.ComputerUrlPath.Replace(@"\",@"/");
495InBlock.gif                fileUrlPath = this.ComputerUrlPath.EndsWith(@"/"== true ? this.ComputerUrlPath : this.ComputerUrlPath + @"/";
496InBlock.gif                fileUrlPath += fileName;
497InBlock.gif
498InBlock.gif                // 判断存放图片文件的目录是否存在,如果不存在,则创建。
499InBlock.gif                DirectoryInfo di = new DirectoryInfo(filePath);
500InBlock.gif                if( di.Attributes == FileAttributes.ReadOnly )
501ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
502InBlock.gif                    string msg = "指定文件夹为只读,不能写入文件";
503InBlock.gif                    client.RegisterStartupScript(cstype, "error""<script language='javascript'>alert('" + msg + "')</script>");
504InBlock.gif                    return;
505ExpandedSubBlockEnd.gif                }

506InBlock.gif                if(!Directory.Exists(filePath))
507InBlock.gif                    Directory.CreateDirectory(filePath);
508InBlock.gif                if( File.Exists( fileFullPath ) && !this.IsOverwrite )
509ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
510InBlock.gif                    string msg = "文件已经存在";
511InBlock.gif                    client.RegisterStartupScript(cstype, "error""<script language='javascript'>alert('" + msg + "')</script>");
512InBlock.gif                    return;
513ExpandedSubBlockEnd.gif                }

514InBlock.gif
515InBlock.gif                // 把文件写入服务器
516InBlock.gif                FileStream newFile = new FileStream(fileFullPath,FileMode.Create);
517InBlock.gif                newFile.Write(bufData,0,fileLen);
518InBlock.gif                newFile.Flush();
519InBlock.gif                newFile.Close();
520InBlock.gif
521InBlock.gif                // 注销远程登陆
522InBlock.gif                ifthis.ComputerDomain != "" )
523InBlock.gif                    classValidUser.UndoImpersonation();
524InBlock.gif
525InBlock.gif                //返回文件路径
526InBlock.gif                this.FileFullUrl = fileUrlPath;
527InBlock.gif                
528InBlock.gif                //保存路径信息
529InBlock.gif                lblMsg.Visible=true;
530InBlock.gif                lblMsg.Text="上载成功";
531InBlock.gif                lblInfo.Visible = true;
532InBlock.gif                double size = (double)fileLen/1024;
533InBlock.gif                lblInfo.Text = "文件名:"+fileName+"&nbsp;&nbsp;&nbsp;文件大小:"+size.ToString("0.00")+"K";
534InBlock.gif
535InBlock.gif                //激发用户自定义事件
536InBlock.gif                if( uploaded != null )
537InBlock.gif                    uploaded(this,new System.EventArgs());
538InBlock.gif
539ExpandedSubBlockEnd.gif            }
                    
540InBlock.gif            catch(Exception ex)
541ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
542InBlock.gif                lblMsg.Visible=true;
543InBlock.gif                lblMsg.Text="上载失败:"+ex.Message;
544InBlock.gif                lblInfo.Visible = false;
545ExpandedSubBlockEnd.gif            }

546ExpandedSubBlockEnd.gif        }

547ExpandedSubBlockEnd.gif        #endregion

548InBlock.gif
549ExpandedSubBlockEnd.gif    }

550ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/nowind/archive/2006/11/27/573415.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值